From aec851e2e1299c53434b50e6de26579ce1a63dbf Mon Sep 17 00:00:00 2001 From: Tom Pietschker Date: Sun, 18 Mar 2012 18:09:22 -0400 Subject: [PATCH 1/3] Add attr_accessible :flag gable, :flagger, :flag to the model. --- lib/make_flaggable/flagging.rb | 1 + spec/models.rb | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/lib/make_flaggable/flagging.rb b/lib/make_flaggable/flagging.rb index 1c71c4b..30024a1 100644 --- a/lib/make_flaggable/flagging.rb +++ b/lib/make_flaggable/flagging.rb @@ -1,5 +1,6 @@ module MakeFlaggable class Flagging < ActiveRecord::Base + attr_accessible :flaggable, :flagger, :flag belongs_to :flaggable, :polymorphic => true belongs_to :flagger, :polymorphic => true scope :with_flag, lambda { |flag| where(:flag => flag.to_s) } diff --git a/spec/models.rb b/spec/models.rb index 3333a38..06ba997 100644 --- a/spec/models.rb +++ b/spec/models.rb @@ -1,3 +1,7 @@ +class Flagging < ActiveRecord::Base + attr_accessible :flaggable, :flagger, :flag +end + class FlaggableModel < ActiveRecord::Base make_flaggable :favorite, :inappropriate end From 54e3d768391a9e64eb2f0e9e3bc4d1df880b0763 Mon Sep 17 00:00:00 2001 From: Tom Pietschker Date: Sun, 18 Mar 2012 18:09:56 -0400 Subject: [PATCH 2/3] Add a matcher for attr_accessible. --- spec/spec_helper.rb | 1 + spec/support/be_accessible_matcher.rb | 8 ++++++++ 2 files changed, 9 insertions(+) create mode 100644 spec/support/be_accessible_matcher.rb diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0478577..aa3aa68 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,6 +4,7 @@ require 'rspec' require 'active_record' require 'database_cleaner' +require 'support/be_accessible_matcher' $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib') require 'make_flaggable' diff --git a/spec/support/be_accessible_matcher.rb b/spec/support/be_accessible_matcher.rb new file mode 100644 index 0000000..1647d67 --- /dev/null +++ b/spec/support/be_accessible_matcher.rb @@ -0,0 +1,8 @@ +RSpec::Matchers.define :allow_mass_assignment_of do |attribute| + match do |response| + response.class.accessible_attributes.include?(attribute) + end + description { "be accessible :#{attribute}" } + failure_message_for_should { ":#{attribute} should be accessible" } + failure_message_for_should_not { ":#{attribute} should not be accessible" } +end From 1bb78004b79a964748b8c50f315c0bc8b9cf6e33 Mon Sep 17 00:00:00 2001 From: Tom Pietschker Date: Sun, 18 Mar 2012 18:11:13 -0400 Subject: [PATCH 3/3] Add a model spec to test attr_accessible. --- spec/models/flagging_spec.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 spec/models/flagging_spec.rb diff --git a/spec/models/flagging_spec.rb b/spec/models/flagging_spec.rb new file mode 100644 index 0000000..4d137af --- /dev/null +++ b/spec/models/flagging_spec.rb @@ -0,0 +1,10 @@ +require 'spec_helper' + +describe Flagging do + describe "attributes should be white listed" do + it { should allow_mass_assignment_of(:flaggable) } + it { should allow_mass_assignment_of(:flagger) } + it { should allow_mass_assignment_of(:flag) } + end +end +