Skip to content

Commit

Permalink
Update has_flags.rb
Browse files Browse the repository at this point in the history
use latest code from jovoto.com app
  • Loading branch information
aka47 committed Apr 30, 2013
1 parent 41c48d1 commit 8053ba7
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions lib/has_flags.rb
Expand Up @@ -18,7 +18,7 @@
# readers: name, name? # readers: name, name?
# writer: name=(v) # writer: name=(v)
# where 'name' is the name of the bit field. the (v) parameter can be true/false, "true"/"false", 0/1, 'yes'/'no', # where 'name' is the name of the bit field. the (v) parameter can be true/false, "true"/"false", 0/1, 'yes'/'no',
# or :yes/:no. specifically, the following (v) inputs evaluate to true: # or :yes/:no. specifically, the following (v) in evaluate to true:
# [ true, 'true', 'yes', :yes, 'ok', :ok, 1, '1' ] # [ true, 'true', 'yes', :yes, 'ok', :ok, 1, '1' ]
# all others, including nil evaluate false. # all others, including nil evaluate false.
# groups_name = [Array of flags] , not the plural # groups_name = [Array of flags] , not the plural
Expand Down Expand Up @@ -86,9 +86,28 @@ def has_flags(bitflags, options = { })


# now declare the class Singleton methods # now declare the class Singleton methods
class_eval { extend RainCity::Has::BitFlags::BitFlagMethods } class_eval { extend RainCity::Has::BitFlags::BitFlagMethods }

# and the named scopes
create_named_scopes bitflags
end end


private private
def create_named_scopes(bitflags)
if (self.bitflag_group)
scope "by_#{self.bitflag_group}", lambda { |flag_title| { :conditions => self.flags_conditions(flag_title) } }
end


bitflags.each do |v|
case v
when Symbol, String
flag_title = v.to_s
scope_name = flag_title
scope scope_name, :conditions => self.flags_conditions(flag_title)
end
end
end

def create_groupflags def create_groupflags
group = self.bitflag_group group = self.bitflag_group
groups = group.pluralize groups = group.pluralize
Expand Down Expand Up @@ -117,7 +136,7 @@ def create_groupflags
end end


define_method( group + "_ids" ) do define_method( group + "_ids" ) do
self.class.flags.collect{|name,id| name }.select{|name| self.send(name.to_sym)} self.class.flags.select{|name,id| self.send(name.to_sym)}.collect{|array| array[1] }
end end


define_method(group + "_ids=") do |ids| define_method(group + "_ids=") do |ids|
Expand All @@ -141,6 +160,7 @@ def create_accessors(bitflags)


default = false default = false
position += 1 position += 1
#ActiveRecord::Base::scope name.to_s.pluralize, lambda{|c| {:conditions => flags_conditions(name)}} # does not allways work, why?
end end


name = v.to_s name = v.to_s
Expand All @@ -162,9 +182,10 @@ def create_accessors(bitflags)
end end


if true if true
define_method( 'after_initialize' ) do self.after_initialize :initialize_flags
initialize_flags # define_method( 'after_initialize' ) do
end # initialize_flags
# end
end end
end end


Expand Down Expand Up @@ -211,7 +232,7 @@ def parse_options(options)
self.bitflag_group = val.to_s self.bitflag_group = val.to_s
#raise RuntimeError, 'Groups not supported for alpha version...' #raise RuntimeError, 'Groups not supported for alpha version...'
else else
raise RuntimeError, "BitFlag: unrecognized option: " + opt.to_s raise RuntimeError, "BitFlag: unrecognized option: #{opt.class} #{options.class}" + opt.to_s
end end
#end #end
end end
Expand Down Expand Up @@ -247,13 +268,13 @@ def find_by_flags(*args)
self.find :all, :conditions => [ conditions ] self.find :all, :conditions => [ conditions ]
end end


def flags_conditions(*args) def flags_conditions(*args)
flags = self.flags flags = self.flags
msk = args.inject(0) do |n, name| msk = args.inject(0) do |n, name|
n += flags[name.to_s] n += flags[name.to_s]
end end
s = sprintf "(%s & %i)=%i", self.bitflag_column, msk, msk s = sprintf "(%s.%s & %i)=%i",self.table_name, self.bitflag_column, msk, msk
end end
end end
end end
end end
Expand Down

0 comments on commit 8053ba7

Please sign in to comment.