Skip to content

Commit

Permalink
fixed ActiveRecord::ReadOnlyRecord error raised when using resources …
Browse files Browse the repository at this point in the history
…instances returned by Resource.with_role method

using .select in resource_adapter to prevent Rails to flag the association as read-only
added specs to cover this use case
closes #175
  • Loading branch information
EppO committed Aug 27, 2013
1 parent 947b469 commit 252a599
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/rolify/adapters/active_record/resource_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def resources_find(roles_table, relation, role_name)
resources = relation.joins("INNER JOIN #{quote(roles_table)} ON #{quote(roles_table)}.resource_type = '#{relation.to_s}' AND
(#{quote(roles_table)}.resource_id IS NULL OR #{quote(roles_table)}.resource_id = #{quote(relation.table_name)}.#{relation.primary_key})")
resources = resources.where("#{quote(roles_table)}.name IN (?) AND #{quote(roles_table)}.resource_type = ?", Array(role_name), relation.to_s)
resources = resources.select("#{quote(relation.table_name)}.*")
resources
end

Expand Down
7 changes: 7 additions & 0 deletions spec/rolify/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,16 @@
it "should include Forum instances with forum role" do
subject.with_role(:forum).should =~ [ Forum.first, Forum.last ]
end

it "should include Forum instances with godfather role" do
subject.with_role(:godfather).should =~ Forum.all.to_a
end

it "should be able to modify the resource", :if => ENV['ADAPTER'] == 'active_record' do
forum_resource = subject.with_role(:forum).first
forum_resource.name = "modified name"
expect(forum_resource.save).not_to raise_error
end
end

context "on the Group class" do
Expand Down

0 comments on commit 252a599

Please sign in to comment.