Ordering with awesome_nested_set - ruby-on-rails

As I have find out awesome_nested_set is the most popular Rails gem for making categories tree. Unfortunately, it hasn't categories ordering function or it isn't docummented.
Maybe anyone knows how to change these categories order?
Menu
- about
- first page
- second page
- contacts
I don't know how to to add some new categories in the middle of the tree.

Say that the node called first page is an instance of the model Page and has the ID 3. You can do the following:
first_page = Page.find(3)
new_page = Page.create(:name => 'new page')
new_page.move_to first_page, :right
This will create a new page called new page and put it between first page and second page.
EDIT: after looking at the code, it appears that there is now also a method move_to_right_of, which might be more convenient.

Related

rails pagy multiple tables on one page

I have two different tables on one page and I want to use pagy on each of them.
For sorting/searching I changed names of each pagy object so I did it like
#pagy_inv, #invoices = pagy #invoices.reorder(sort_column_show_city_invoices => sort_direction_show_city_invoices), items: params.fetch(:count, 10)
and
#pagy_ord, #orders = pagy #orders.reorder(sort_column_show_city_orders => sort_direction_show_city_orders), items: params.fetch(:count, 10)
which works fine for sorting and searching, but when I try to change page, it sends page parameter and tries to change page for every table and therefore if one table has like 5 pages and another 10 and I try to move to page 10 it fails because one of the table cannot move to not existing table.
I was thinking to change page param (for example page_inv and page_ord) but how to do that? Or is there some easy way how to change page only for table from selected pagy object?
As you already understood, pagy use a default :page_param (which is :page): if you use more than one instance in the same page you should differentiate the :page_param of different instances or they will use all the same.
You can pass that in the pagy method (as you did with :items).

Creating Table Filters

I have a simple table with 3 columns like so
table
thead
th Element
th Owner
th Progress
tbody
== render :partial => "element_row" :collection => #elements :as => table_element
Each element is unique, and one owner may have several elements. There are a few different types of "progress" e.g. "started", "not started", and "completed".
I want to create a few links that filter the table. For example, I want to create an started link where when the user clicks on the link, the table is filtered down to only show rows where "started" is displayed. Another example of a filter is by owner.
Does anyone have any suggestions for how to do this?
If you're looking for a gem to help you, Ransack is a great, popular, and active project for searching (and by extension, filtering) ActiveRecord data.
If you check out Ernie's demo site you can see how the search parameters modify the URL through GET queries. You could easily create links like your desired started link to mock these GET form requests.
If you want to do this on the server side, add a filter method to the controller that uses link parameters to filter the #elements.
If you selecting #elements from the database using ActiveRecord, you could do:
#elements = Element.where(progress: params[:progress])
If you just want to filter the #elements in memory, you could do:
#elements = #elements.select{ |element| element.progress == params[:progress] }

Extra, unwanted field from haml block rails partial

I'm outputting a collection in haml
#- if #fields.count>0
.sfields
#= render :partial=>"sfields/field", :collection=>#fields, :as=>:field
= render #fields
When there are zero fields, there is always one phantom field rendered. When I add 'if #fields.count>0' then when there is zero there is no output, but as soon as I add one field I get two rendered, the one I added and a phantom field.
This is the first I've run into this and I'm not sure what I did that is outputting a phantom field. I remember seeing something similar, but can't remember where I saw it, and my search turned up nothing relevant atm.
EDIT 1:
Contents of partial
.sfield
= field.name
.fielditems
#- fields.items each do |i|
#= render :partial=>"items/item", :locals=>{:i => i}
= render field.items
So, there is always an extra sfield hanging around.
EDIT 2
Abstractly, I have this:
Show action -> partial for fields -> partial for field items -> partial for field item choices, 4 different places pulled into one page.
So fields is a collection of items and an item is a collection of choices. Even after going through and making the renders more rails default (e.g. render #fields, render field.items, etc.) I still have that phantom field. I don't want to hide it with js or something (ack awful), so I need to figure some solution which entails more trial and error and reading.
It works, but is ugly with the phantom field hanging on.
EDIT 3
I've taken several steps back combined everything into one template, sans partials. Still there. Something basic I'm doing wrong or missing is adding this phantom field, so annoying. It isn't necessarily the partials then.
EDIT 4: SOLVED
refined the #fields select in the controller to select only fields attached to the item I'm showing
e.g.
#fields = #showing.fields
to
#fields = #showing.fields(:showing_id=>#showing.id)
I'd still like to know why it was doing that grr...
Your #fields wouldn't be a list of hashes would it?
NOTE: Due to backwards compatibility concerns, the collection can’t be one of hashes. Normally you’d also just keep domain objects, like Active Records, in there.
-- http://api.rubyonrails.org/classes/ActionView/Partials.html
Note EDIT 4: SOLVED
refined the #fields select in the controller to select only fields attached to the item I'm showing
e.g.
#fields = #showing.fields
to
#fields = #showing.fields(:showing_id=>#showing.id)

break down a complex search query in Rails 3

I have a controller which has a lot of options being sent to it via a form and I'm wondering how best to separate them out as they are not all being used simultaneously. Ie sometimes no, tags, sometimes no price specified. For prices I have a default price set so I can work around with it always being there, but the tags either need to be there, or not. etc.
#locations = Location.find(params[:id])
#location = #locations.places.active.where("cache_price BETWEEN ? AND ?",price_low,price_high).tagged_with([params[:tags]).order(params[:sort]).paginate :page => params[:page]
I haven't seen any good examples of this, but I'm sure it must happen often... any suggestions? Also, even will_paginate which gets tacked on last should be optional as the results either go to a list or to a google map, and the map needs no pagination.
the first thing to do when refactoring a complex search action is to use an anonymous scope.
Ie :
fruits = Fruit.scoped
fruits = fruits.where(:colour => 'red') if options[:red_only]
fruits = fruits.where(:size => 'big') if options[:big_only]
fruits = fruits.limit(10) if options[:only_first]
...
If the action controller still remains too big, you may use a class to handle the search. Moreover, by using a class with Rails 3 and ActiveModel you'll also be able to use validations if you want...
Take a look at one of my plugins : http://github.com/novagile/basic_active_model that allows you to easily create classes that may be used in forms.
Also take a look at http://github.com/novagile/scoped-search another plugin more specialized in creating search objects by using the scopes of a model.

Record id with cucumber and pickle [Rails]

I am using Cucumber, Webrat, and Pickle in conjunction.
When I write a scenario, I can do something like this:
Given a product exists with title: "Bread"
When I go to the edit page for that product
And I fill in "Title" with "Milk"
And I press "Save changes"
Then I should see "Successfully edited product."
And I should be on that car's page
Notice the for that product. This is something pickle provides which is very convenient for referencing the record for a product I'm checking the existence of. That last line, though, is not working.
Basically I am trying to make sure I am the show page for that record, but since I do not have an ID for it, I don't know how to reference it.
Any help?
Thanks!
To have a reference to the created product or anything else you can use naming that's provided by pickle:
Given product: "bread" exists with title: "Bread"
...
Then I should be on the showing page for the product "bread"
To handle this url you will need to add couple lines into /features/support/paths.rb:
when %r{^the showing page for the (.+)$}
polymorphic_path(model($1))
Also it could be useful to handle edit path for the model like this:
Then I should be on the edit page for the product "bread"
paths.rb:
when %r{^the edit page for the (.+)$}
polymorphic_path(model($1), :action => 'edit')

Resources