Skip to content
This repository
Browse code

Bugfixes

  • Loading branch information...
commit 52880b4b63851f14565adde29cdce00594c437e0 1 parent ecae1a0
Daniel Davey jeriko authored

Showing 2 changed files with 15 additions and 11 deletions. Show diff stats Hide diff stats

  1. +12 8 lib/methods/serialize.rb
  2. +3 3 spec/easy_roles_spec.rb
20 lib/methods/serialize.rb
@@ -15,13 +15,18 @@ def initialize(base, column_name, options)
15 15 base.send :define_method, :add_role do |role|
16 16 clear_roles if self[column_name.to_sym].blank?
17 17
  18 + marker = base::ROLES_MARKER
  19 + return false if (!marker.empty? && role.include?(marker))
  20 +
18 21 has_role?(role) ? false : self[column_name.to_sym] << role
19 22 end
20 23
21 24 base.send :define_method, :add_role! do |role|
22   - return false if !base::ROLES_MARKER.empty? && role.include?(base::ROLES_MARKER)
23   - add_role(role)
24   - self.save!
  25 + if add_role(role)
  26 + self.save!
  27 + else
  28 + return false
  29 + end
25 30 end
26 31
27 32 base.send :define_method, :remove_role do |role|
@@ -65,6 +70,10 @@ def initialize(base, column_name, options)
65 70
66 71 base.class_eval do
67 72 const_set :ROLES_MARKER, '!'
  73 + scope :with_role, proc { |r|
  74 + query = "#{self.table_name}.#{column_name} LIKE " + ['"%',base::ROLES_MARKER,r,base::ROLES_MARKER,'%"'].join
  75 + where(query)
  76 + }
68 77
69 78 define_method :add_role_markers do
70 79 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)
79 88 after_save :strip_role_markers
80 89 after_rollback :strip_role_markers
81 90 after_find :strip_role_markers
82   -
83   - scope :with_role, proc { |r|
84   - query = "#{self.table_name}.#{column_name} LIKE " + ['"%',base::ROLES_MARKER,r,base::ROLES_MARKER,'%"'].join
85   - where(query)
86   - }
87 91 end
88 92
89 93 end
6 spec/easy_roles_spec.rb
@@ -198,7 +198,6 @@
198 198 it "should prove that wrapper markers are a necessary strategy by failing without them" do
199 199 marker_cache = SerializeUser::ROLES_MARKER
200 200 SerializeUser::ROLES_MARKER = ''
201   - (chuck = SerializeUser.create(name: 'Mr. Norris')).add_role!('recursion')
202 201 (morgan = SerializeUser.create(name: 'Mr. Freeman')).add_role!('onrecursionrecursi')
203 202 SerializeUser.with_role('recursion').include?(morgan).should be_true
204 203 SerializeUser::ROLES_MARKER = marker_cache
@@ -207,15 +206,16 @@
207 206 it "should avoid incorrectly matching roles where the name is a subset of another role's name" do
208 207 (chuck = SerializeUser.create(name: 'Mr. Norris')).add_role!('recursion')
209 208 (morgan = SerializeUser.create(name: 'Mr. Freeman')).add_role!('onrecursionrecursi')
  209 + SerializeUser.with_role('recursion').include?(chuck).should be_true
210 210 SerializeUser.with_role('recursion').include?(morgan).should be_false
211 211 end
212 212
213 213 it "should not allow roles to be added if they include the ROLES_MARKER character" do
214 214 marker_cache = SerializeUser::ROLES_MARKER
215   - SerializeUser::MARKER = '!'
  215 + SerializeUser::ROLES_MARKER = '!'
216 216 user = SerializeUser.create(name: 'Towelie')
217 217 user.add_role!('funkytown!').should be_false
218   - SerializeUser::MARKER = marker_cache
  218 + SerializeUser::ROLES_MARKER = marker_cache
219 219 end
220 220
221 221 it "should correctly handle markers on failed saves" do

0 comments on commit 52880b4

Please sign in to comment.
Something went wrong with that request. Please try again.