Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Bugfixes

  • Loading branch information...
commit 52880b4b63851f14565adde29cdce00594c437e0 1 parent ecae1a0
@jeriko jeriko authored
Showing with 15 additions and 11 deletions.
  1. +12 −8 lib/methods/serialize.rb
  2. +3 −3 spec/easy_roles_spec.rb
View
20 lib/methods/serialize.rb
@@ -15,13 +15,18 @@ def initialize(base, column_name, options)
base.send :define_method, :add_role do |role|
clear_roles if self[column_name.to_sym].blank?
+ marker = base::ROLES_MARKER
+ return false if (!marker.empty? && role.include?(marker))
+
has_role?(role) ? false : self[column_name.to_sym] << role
end
base.send :define_method, :add_role! do |role|
- return false if !base::ROLES_MARKER.empty? && role.include?(base::ROLES_MARKER)
- add_role(role)
- self.save!
+ if add_role(role)
+ self.save!
+ else
+ return false
+ end
end
base.send :define_method, :remove_role do |role|
@@ -65,6 +70,10 @@ def initialize(base, column_name, options)
base.class_eval do
const_set :ROLES_MARKER, '!'
+ scope :with_role, proc { |r|
+ query = "#{self.table_name}.#{column_name} LIKE " + ['"%',base::ROLES_MARKER,r,base::ROLES_MARKER,'%"'].join
+ where(query)
+ }
define_method :add_role_markers do
self[column_name.to_sym].map! { |r| [base::ROLES_MARKER,r,base::ROLES_MARKER].join }
@@ -79,11 +88,6 @@ def initialize(base, column_name, options)
after_save :strip_role_markers
after_rollback :strip_role_markers
after_find :strip_role_markers
-
- scope :with_role, proc { |r|
- query = "#{self.table_name}.#{column_name} LIKE " + ['"%',base::ROLES_MARKER,r,base::ROLES_MARKER,'%"'].join
- where(query)
- }
end
end
View
6 spec/easy_roles_spec.rb
@@ -198,7 +198,6 @@
it "should prove that wrapper markers are a necessary strategy by failing without them" do
marker_cache = SerializeUser::ROLES_MARKER
SerializeUser::ROLES_MARKER = ''
- (chuck = SerializeUser.create(name: 'Mr. Norris')).add_role!('recursion')
(morgan = SerializeUser.create(name: 'Mr. Freeman')).add_role!('onrecursionrecursi')
SerializeUser.with_role('recursion').include?(morgan).should be_true
SerializeUser::ROLES_MARKER = marker_cache
@@ -207,15 +206,16 @@
it "should avoid incorrectly matching roles where the name is a subset of another role's name" do
(chuck = SerializeUser.create(name: 'Mr. Norris')).add_role!('recursion')
(morgan = SerializeUser.create(name: 'Mr. Freeman')).add_role!('onrecursionrecursi')
+ SerializeUser.with_role('recursion').include?(chuck).should be_true
SerializeUser.with_role('recursion').include?(morgan).should be_false
end
it "should not allow roles to be added if they include the ROLES_MARKER character" do
marker_cache = SerializeUser::ROLES_MARKER
- SerializeUser::MARKER = '!'
+ SerializeUser::ROLES_MARKER = '!'
user = SerializeUser.create(name: 'Towelie')
user.add_role!('funkytown!').should be_false
- SerializeUser::MARKER = marker_cache
+ SerializeUser::ROLES_MARKER = marker_cache
end
it "should correctly handle markers on failed saves" do
Please sign in to comment.
Something went wrong with that request. Please try again.