module EntityStatus::ClassMethods
Public Instance Methods
create_finder_methods(column_name,name)
click to toggle source
# File lib/entity_status.rb, line 62 def create_finder_methods(column_name,name) klass = self.to_s metaclass.instance_eval do define_method(name){ where(column_name.to_sym => name) } end end
create_statuses_method(name,statuses)
click to toggle source
# File lib/entity_status.rb, line 51 def create_statuses_method(name,statuses) klass = self.to_s metaclass.instance_eval do # attr_accessor :statuses # @@statuses = statuses define_method(name){ return statuses } end end
entity_status(column_name="status",status_array = [], options = { destroyed_status: nil})
click to toggle source
# File lib/entity_status.rb, line 12 def entity_status(column_name="status",status_array = [], options = { destroyed_status: nil}) status_array = (status_array.count > 0) ? status_array : %W(pending open closed) if options[:destroyed_status] status_array << options[:destroyed_status] self.set_default_scope(column_name, options[:destroyed_status]) define_method 'destroyed_status' do options[:destroyed_status] end end self.create_statuses_method(column_name.to_s.pluralize,status_array) status_array.each do |st| self.create_finder_methods(column_name,st) # add dynamic state setters based on the status string # example: Post.first.pending #=> 'pending' define_method st do self.send("#{column_name}=".to_sym,st.to_s) self end # Add boolean state checks based on the status string # example: Post.first.pending? #=> true define_method "#{st}?" do (self.send(column_name) == st) ? true :false end end end
set_default_scope(column_name,destroyed_status)
click to toggle source
# File lib/entity_status.rb, line 43 def set_default_scope(column_name,destroyed_status) self.default_scope {where.not(column_name.to_sym => destroyed_status)} # klass = self.to_s # metaclass.instance_eval do # default_scope where.not(column_name.to_sym => destroyed_status) # end end