How to avoid grand_parent id in 2 level nested routes in rails? - ruby-on-rails-4.2

I my routes file:
resources :users do
resources :posts, shallow: true do
resources :comments, shallow: true
end
end
Now, routes for comments for index seems like below :-
"/users/:user_id/posts/:post_id/comments"
Now, I would like to get only below type url:-
"/posts/:post_id/comments"
ie. I would like to skip the grand_parent portion. How can I achieve this, Thanks.

Simply putting shallow: true option in grand_parent gave the solution as:
resources :users, shallow: true do
resources :posts do
resources :comments
end
end

Related

how to use ternary operator in mustache template

I have a scenario where I am getting {{isdone}} value with Boolean data.
I want to be printed as "pending" for false value and "Done" for true.
I'm using below code, Which isn't working.
{{isdone}} == false ? "pending" : "Done"
Use the ^ block for else.
You can (now) use the ^ block for an else or false condition. Something like this should work:
{{#isdone}}Done{{/isdone}}{{^isdone}}pending{{/isdone}}
Or as a more readable multi-line block of code:
{{#isdone}}
Done
{{/isdone}}
{{^isdone}}
pending
{{/isdone}}
As long as you have control of your context data, correct way is to pass another variable, that will already contain pending or Done beforehand.
If you don't have control over the data, then maybe moustache isn't good for you as you may need template engine that can have some more logic in it to transform data a bit.
You might want to register a ternary helper for that
Handlebars.registerHelper("ternary", function (condition, trueValue, falseValue, options) {
return condition ? trueValue : falseValue;
});
and then in your templates use it like
{{ternary isdone "Done" "pending"}}

Origen test_ids next in range accept a Proc/Lambda?

I am working with the Origen test_ids gem 'next in range' feature. When I setup the softbin configuration in the test interface I find out dynamically how many different hardbins have a unique softbin range. This is known but it varies depending on the test module being tested. Some test modules may have 3 hardbin to softbin combinations and some have 5. Is it possible to pass a Proc/Lambda to the softbin config shown below?
config.softbins needs: :bin do |options|
    if options[:bin] == 1
      TestIds.next_in_range((1000..2000))
    elsif options[:bin] == 11
      TestIds.next_in_range((10000..99999))
    end
  end
Such that the number of elsif statements, the bin and the softbin range are all dynamically stitched together. I know eval could work but it seems to be frowned upon.
EDIT
OK after reviewing Ginty's answer I tried the solution but it seems like the options are not getting passed into the next_in_range method. Here is the config:
TestIds.configure current_test_insertion do |config|
config.bins.include << binning_obj.configs(:all).hbin
config.softbins needs: :bin do |options|
bin_map = Hash[test_type_hardbins.zip(binning_test_types)]
TestIds.next_in_range(bin_map[options[:bin]])
end
config.send_to_ate = false
end
Here is the error:
COMPLETE CALL STACK
-------------------
wrong number of arguments (given 1, expected 2)
/users/user/origen/github/test_ids/lib/test_ids.rb:236:in `next_in_range'
When I pass in the options as so:
TestIds.next_in_range(bin_map[options[:bin]], options)
I get this error:
COMPLETE CALL STACK
-------------------
undefined method `map' for nil:NilClass
Did you mean? tap
/users/user/origen/github/test_ids/lib/test_ids/allocator.rb:45:in `range_item'
/users/user/origen/github/test_ids/lib/test_ids/allocator.rb:32:in `next_in_range'
Given that the docs say this feature is in beta, should I move this to a Github issue?
thx
When defining a softbin with a block, you have complete freedom to put whatever you want in the block, so adding an additional Proc into the equation doesn't make sense to me.
There are effectively two APIs here that you can combine, one is the ability to define a function to work out the number:
config.softbins do |options|
# Put any logic you like in here, return the number at the end
end
The other API is the ability to have TestIds keep track of a position in a range:
TestIds.next_in_range((1000..2000))
You can use that, or not, within your block however you wish.
That you should give you full freedom to define whatever rules you like:
config.softbins needs: bin do |options|
if Time.now.tuesday?
bin_map = { 5: (1..10), 11: (11..20) }
else
bin_map = { 6: (10..20), 12: (21..30) }
end
TestIds.next_in_range(bin_map[options[:bin]])
end
Note that if you refer to the same next_in_range within different branches then they will both consume from the same set of numbers.
If you wanted them to each independently count within that range, then you would need to setup different configurations so that they each have their own database:
if Time.now.tuesday?
TestIds.configure :rule1 do |config|
end
else
TestIds.configure :rule2 do |config|
end
end

True and False in Alloy

Alloy has plenty of logical connectives like and and or and implies. But I can't find true and false. Are they missing? At the moment I've been making do with 1=1 and 1=0, but this is rather hacky (and gives a compiler warning).
My reason, by the way, for wanting true and false is that I'm writing something that produces an .als file. My top-level .als file expects that my auto-generated .als file defines a wellformed predicate and a faulty predicate. Sometimes these predicates are complicated, but sometimes I just want wellformed[...] to return true, and faulty[...] to return false. This is why I want true and false in the Alloy language.
They're not built in for a good reason: see the FAQ on p137 of Software Abstractions (Daniel Jackson, MIT Press, 2012). The issue in short is that if they were built in, you'd have to be able to declare a relation over the booleans, and then because boolean expressions could evaluate to {} and {T,F}, the connectives would need to be defined over these values, and that seemed like a really bad idea.
Since an empty predicate is true, my favorite implementation of true and false is:
pred true {}
pred false { not true }
pred true {no none}
pred false {some none}
seems to work; but it would be nice to have these inbuilt.

How do I make Active Admin show code DRY across models?

I have a number of models within Active Admin that have very similar (but not exactly the same) show pages along the lines of:
show do |ad|
attributes_table do
row :name
row :length
row :width
row :height
...
end
panel "Images" do
text_node link_to 'Add Image', new_admin_image_path(...)
table_for ad.images do
column "Image" do |image|
image_tag(...)
end
column do |data|
link_to :edit, edit_admin_image_path(...)
end
column do |data|
link_to :delete, admin_image_path(data), method: :delete
end
end
end
end
The 'panel "Images" do' code will be duplicated exactly within each model, so I'd like to put it somewhere else. I've been going down the ViewHelper and render partial paths, but in both cases I end up with something that doesn't know what "panel", "text_node", "table_for", etc. is. Guidance as to what is the right way to do this?
Arbre, the template language ActiveAdmin uses, does support partials. You can
move the duplicated code into an arb partial such as
app/views/admin/_images_panel.html.arb. Then your ActiveAdmin resources
can simply call render with the partial path and any needed local
variables.
show do
attributes_table do
# ...
end
render 'admin/images_panel', data: data
end
The partial may also reference the generic method resource to eliminate
the need to pass in local variables. Resource is whatever resource the
admin is managing. For example:
panel "Images" do
table_for resource.images do
# Note use of `resource` instead of `ad` above.
# ...
end
end

How to check the value of a boolean (set by user) with a variable string?

The user sets a boolean to true or false.
That does (exemple)
ElementNameone = true
ElementNametwo = false
ElementNamethree = true
Etc.
Now I have a string that is loaded from a file. The string called name can have values that are Nameone, Nametwo, Namethree, etc. Anyone of them at a time.
Now I would like to be able to do this
if Element .. name == true then
do something
Except I don't know how to do this properly.
I've tried to do
if not not ("Element" .. name) then
But it does not work.
Can anyone help ?
Thanks
Try this:
if _G["Element" .. name] == true then
-- do something
end
Note that this will work only if the variables set by the user (ElementNameone, .. etc.) are globals.
It's very likely you're solving the wrong problem.
You say "the user" sets these variables. How? An end user normally isn't going to be interacting directly with variables inside your program.
Can you use a table instead, with ElementNameone as the key and true or false as the associated value? If so, that would be a lot cleaner.

Resources