Skip to content

Commit

Permalink
! fix for comparison of Array with Array failed in sortable when upda…
Browse files Browse the repository at this point in the history
…ting existing records
  • Loading branch information
beatrichartz committed Apr 3, 2014
1 parent ee996b0 commit 33f5c74
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 16 deletions.
6 changes: 3 additions & 3 deletions features/index/format_as_csv.feature
Expand Up @@ -12,8 +12,8 @@ Feature: Format as CSV
When I am on the index page for posts
And I follow "CSV"
And I should download a CSV file for "posts" containing:
| Id | Title | Body | Published at | Starred | Created at | Updated at |
| \d+ | Hello World | | | | (.*) | (.*) |
| Id | Title | Body | Published at | Position | Starred | Created at | Updated at |
| \d+ | Hello World | | | | | (.*) | (.*) |

Scenario: Default with alias
Given a configuration of:
Expand All @@ -24,7 +24,7 @@ Feature: Format as CSV
When I am on the index page for my_articles
And I follow "CSV"
And I should download a CSV file for "my-articles" containing:
| Id | Title | Body | Published at | Starred | Created at | Updated at |
| Id | Title | Body | Published at | Position | Starred | Created at | Updated at |

Scenario: With CSV format customization
Given a configuration of:
Expand Down
12 changes: 6 additions & 6 deletions features/index/index_as_table.feature
Expand Up @@ -180,14 +180,14 @@ Feature: Index as Table
"""
When I am on the index page for posts
Then I should see the "index_table_posts" table:
| [ ] | Id | Title | Body | Published At | Starred | Created At | Updated At | |
| [ ] | 2 | Bye bye world | Move your... | | No | /.*/ | /.*/ | ViewEditDelete |
| [ ] | 1 | Hello World | From the body | | No | /.*/ | /.*/ | ViewEditDelete |
| [ ] | Id | Title | Body | Published At | Position | Starred | Created At | Updated At | |
| [ ] | 2 | Bye bye world | Move your... | | | No | /.*/ | /.*/ | ViewEditDelete |
| [ ] | 1 | Hello World | From the body | | | No | /.*/ | /.*/ | ViewEditDelete |
When I follow "Id"
Then I should see the "index_table_posts" table:
| [ ] | Id | Title | Body | Published At | Starred | Created At | Updated At | |
| [ ] | 1 | Hello World | From the body | | No | /.*/ | /.*/ | ViewEditDelete |
| [ ] | 2 | Bye bye world | Move your... | | No | /.*/ | /.*/ | ViewEditDelete |
| [ ] | Id | Title | Body | Published At | Position | Starred | Created At | Updated At | |
| [ ] | 1 | Hello World | From the body | | | No | /.*/ | /.*/ | ViewEditDelete |
| [ ] | 2 | Bye bye world | Move your... | | | No | /.*/ | /.*/ | ViewEditDelete |

Scenario: Sorting by a virtual column
Given a post with the title "Hello World" exists
Expand Down
7 changes: 5 additions & 2 deletions lib/active_admin/form_builder.rb
Expand Up @@ -83,8 +83,11 @@ def has_many(assoc, options = {}, &block)
# make sure that the sortable children sorted in stable ascending order
if column = builder_options[:sortable]
children = object.send(assoc)
children = children.sort_by {|o| [o.send(column), o.id]}
options[:for] = [assoc, children]
children.sort_by! do |o|

This comment has been minimized.

Copy link
@jgorset

jgorset Apr 9, 2014

Contributor

Hi buddy! It turns out sort_by! is blacklisted as of rails/rails@d4ee09c to "prevent odd bugs and confusion in code that call mutator methods directely on the Relation". I changed it to sort_by in #3064.

attribute = o.send(column)
[attribute.nil? ? Float::INFINITY : attribute, o.id || Float::INFINITY]
end
options[:for] = [assoc, children]
end

html = without_wrapper do
Expand Down
4 changes: 2 additions & 2 deletions spec/support/rails_template.rb
Expand Up @@ -12,14 +12,14 @@
gsub_file 'config/database.yml', /\z/, "\ncucumber:\n <<: *test\n database: db/cucumber.sqlite3"
gsub_file 'config/database.yml', /\z/, "\ncucumber_with_reloading:\n <<: *test\n database: db/cucumber.sqlite3"

generate :model, "post title:string body:text published_at:datetime author_id:integer custom_category_id:integer starred:boolean"
generate :model, "post title:string body:text published_at:datetime author_id:integer position:integer custom_category_id:integer starred:boolean"
inject_into_file 'app/models/post.rb', %q{
belongs_to :category, foreign_key: :custom_category_id
belongs_to :author, class_name: 'User'
has_many :taggings
accepts_nested_attributes_for :author
accepts_nested_attributes_for :taggings
attr_accessible :author unless Rails::VERSION::MAJOR > 3 && !defined? ProtectedAttributes
attr_accessible :author, :position unless Rails::VERSION::MAJOR > 3 && !defined? ProtectedAttributes
}, after: 'class Post < ActiveRecord::Base'
copy_file File.expand_path('../templates/post_decorator.rb', __FILE__), "app/models/post_decorator.rb"

Expand Down
4 changes: 2 additions & 2 deletions spec/unit/filters/resource_spec.rb
Expand Up @@ -13,7 +13,7 @@

it "should return the defaults if no filters are set" do
expect(resource.filters.keys).to match_array([
:author, :body, :category, :created_at, :published_at, :starred, :taggings, :title, :updated_at
:author, :body, :category, :created_at, :position, :published_at, :starred, :taggings, :title, :updated_at
])
end

Expand Down Expand Up @@ -97,7 +97,7 @@
resource.add_filter :count, as: :string

expect(resource.filters.keys).to match_array([
:author, :body, :category, :count, :created_at, :published_at, :starred, :taggings, :title, :updated_at
:author, :body, :category, :count, :created_at, :position, :published_at, :starred, :taggings, :title, :updated_at
])
end

Expand Down
41 changes: 40 additions & 1 deletion spec/unit/form_builder_spec.rb
Expand Up @@ -437,7 +437,7 @@ def build_form(options = {}, form_object = Post.new, &block)
let :body do
build_form({url: '/categories'}, Category.new) do |f|
f.object.posts.build
f.has_many :posts, sortable: :custom_category_id do |p|
f.has_many :posts, sortable: :position do |p|
p.input :title
end
end
Expand All @@ -448,6 +448,45 @@ def build_form(options = {}, form_object = Post.new, &block)
end

end

context "with post returning nil for the sortable attribute" do
let :body do
build_form({url: '/categories'}, Category.new) do |f|
f.object.posts.build position: 3
f.object.posts.build
f.has_many :posts, sortable: :position do |p|
p.input :title
end
end
end

it "shows the nested fields for unsaved records" do
body.should have_tag("fieldset", attributes: {class: "inputs has_many_fields"})
end

end

context "with existing and new posts" do
let! :category do
Category.create name: 'Name'
end
let! :post do
category.posts.create
end
let :body do
build_form({url: '/categories'}, category) do |f|
f.object.posts.build
f.has_many :posts, sortable: :position do |p|
p.input :title
end
end
end

it "shows the nested fields for saved and unsaved records" do
body.should have_tag("fieldset", attributes: {class: "inputs has_many_fields"})
end

end
end

describe "with nesting" do
Expand Down

0 comments on commit 33f5c74

Please sign in to comment.