How to put an element between posts? - html

I'm developing an app that is similar to a blog and I need to put a different element (advertising) from the posts after three posts in every page. To explain better what I wanna do I'll try to illustrate:
[ POST ]
[ POST ]
[ POST ]
[ ELEMENT]
[ POST ]
[ POST ]
[ POST ]
[ ... ]
In every page I want to follow this pattern without interrupting the for cycle and without using any Javascript or CSS hack, just a server-side trick to do this.

what about using an index on your each, and the checking for the modulus ?
Posts.each_with_index do |post,index|
[ Post _content_ here ]
if index % 3 == 0
[Element content here ]
end
end

Related

JSONPath to get multiple values from nested json

I have a nested JSON in a field that contains multiple important keys that I would like to retrieve as an array:
{
"tasks": [
{
"id": "task_1",
"name": "task_1_name",
"assignees": [
{
"id": "assignee_1",
"name": "assignee_1_name"
}
]
},
{
"id": "task_2",
"name": "task_2_name",
"assignees": [
{
"id": "assignee_2",
"name": "assignee_2_name"
},
{
"id": "assignee_3",
"name": "assignee_3_name"
}
]}]}
All the queries that I've tried so far fx ( $.tasks.*.assignees..id) and many others have returned
[
"assignee_1",
"assignee_2",
"assignee_3"
]
But what I need is:
[
["assignee_1"],
["assignee_2", "assignee_3"]
]
Is it possible to do with JSONPath or any script inside of it, without involving 3rd party tools?
The problem you're facing is that tasks and assignees are arrays. You need to use [*] instead of .* to get the items in the array. So your path should look like
$.tasks[*].assignees[*].id
You can try it at https://json-everything.net/json-path.
NOTE The output from my site will give you both the value and its location within the original document.
Edit
(I didn't read the whole thing :) )
You're not going to be able to get
[
["assignee_1"],
["assignee_2", "assignee_3"]
]
because, as #Tomalak mentioned, JSON Path is a query language. It's going to remove all structure and return only values.

Catch and handle (differently) different exceptions for the same block, with an ensure

I know from the MOOC documentation that it's possible to have the same handler for multiple exceptions that can occur during some block, e.g.:
[ do some work ]
on: ZeroDivide, Warning
do: [ :ex | what you want ]
In the same document, there's an example with ensure to make sure code is always executed (despite any exceptions):
[ doSomething ] ensure: [ alwaysExecuteThis ]
However, I would like something like:
[ do some work ]
on: ZeroDivide
do: [ :zeroDivide | handle it ]
on: Warning
do: [ :warning | handle it ]
ensure: [ alwaysExecuteThis ]
Admittedly, this is my Java experience influencing how I want to use Pharo.
It seems it's possible using nested blocks:
[ [ [ [ doSomething here ]
on: ZeroDivide
do: [ :zeroEx | 'zeroExc' crLog ] ]
on: Warning
do: [ :warning | 'warning' crLog ] ]
ensure: [ 'ensure' crLog ] ]
Try adding this method to BlockClosure:
on: exception1
do: block1
on: exception2
do: block2
^self on: exception1 , exception2 do: [:ex |
(exception1 handles: ex)
ifTrue: [block1 value: ex]
ifFalse: [block2 value: ex]]
Note that exception1 and exception2 can be subclasses of Exception or instances of ExceptionSet (created with #,).

sql data convert to json

Below is my query result. i want the json out put as :
[
{id:1,
name:a,
category : [
{name:balakrishnan},
{name:nikhil}
]
},
{id:2,
name:b,
category : [
{name:midhun},
{name:amith}
]
}
]
I have done this with async parallel, i just want to know where we can do in another approach like map functions

What is the most efficient way to patch a JSON patch?

Say, I have this JSON patch, which adds something into the groups array
[
{"op":"add","path":"/food/groups/-",
"value":{
"select" : 55,
"pool" : [
[ 0.111, "biscuit" ],
[ 0.111, "cookie" ],
[ 0.111, "pretzel" ]
]
}
}
]
I need to make a patch to this patch, that removes [ 0.111, "pretzel" ]. Normally I can just utilize the operation remove.
Problem is, I cannot be so sure which number that biscuit/cookie/pretzel patch will come up. I don't know if it'll be the 2nd in the array, or the 5th, because users might apply another patch that adds something into the array.
What would be the most efficient way to solve this problem, without making an exact copy of the patch but without the pretzel?

add to existing object in json array Rails 3

render :json mailbox.inbox.as_json(:include => [
{:messages => {:only => [:body]}},
{:recipients => {:only => [:Firstname,:Surname]}}
])
produces the following output from my Rails controller:
[
{
"conversation":
{
"subject":"Subject for conversation 2",
"messages" :[
{"body":"Message 1"},
{"body":"Message 2"}
],
"recipients": [
{"Firstname":"James","Surname":"Smith"},
{"Firstname":"John","Surname":"Doe"}
]
}
}
]
What I would like to do is to add a current_user key:value pair to each conversation object so the out sample would look like this:
[
{
"conversation":
{
"subject":"Subject for conversation 2",
"messages" :[
{"body":"Message 1"},
{"body":"Message 2"},
],
"recipients": [
{"Firstname":"James","Surname":"Smith"},
{"Firstname":"John","Surname":"Doe"}
],
"current_user" :[
{"id":10}
]
}
}
]
I don't have access to the conversation model in question (it's running off the mailboxer gem) so can't override as_json. Any help would be appreciated
So, formalizing the comments.
By looking the apidock of json_as, you can see that the only way to include arbitrary data is to add a method that retunrs the data you want to use in the model.
Since it is not the best thing to do in many ways and you don't have access to the model, one way out to customize your json is using a popular gem called jbuilder, that allows you to create a more complex json.