Module: IAmICan::DynamicGenerate
- Extended by:
- DynamicGenerate
- Included in:
- DynamicGenerate
- Defined in:
- lib/i_am_i_can/dynamic_generate.rb
Instance Method Summary collapse
Instance Method Details
#assignment_helpers ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/i_am_i_can/dynamic_generate.rb', line 45 def assignment_helpers # Generate 4 methods for each Content of Assignment # # Example for a subject model called User, which `has_and_belongs_to_many :stored_roles`. # You call this proc by given contents [:role], then: # # 1. stored_roles_add # Add roles to a user instance # # 2. stored_roles_add # Remove roles to a user instance # # 3. stored_role_names # Get names of stored_roles of a user instance # # 4. self.stored_role_names # Get names of stored_roles of User ActiveRecord::Relation # proc do |contents| contents.each do |content| # TODO: refactoring define_method "#{_reflect_of(content)}_add" do |locate_vals = nil, check_size: nil, **condition| condition = { name: locate_vals } if locate_vals assoc = send("_#{content.to_s.pluralize}") records = i_am_i_can.send("#{content}_model").where(condition).where.not(id: assoc.ids) # will return false if it does nothing return false if records.blank? || (check_size && records.count != check_size) assoc << records end define_method "#{_reflect_of(content)}_rmv" do |locate_vals = nil, check_size: nil, **condition| condition = { name: locate_vals } if locate_vals assoc = send("_#{content.to_s.pluralize}") records = i_am_i_can.send("#{content}_model").where(id: assoc.ids, **condition) # will return false if it does nothing return false if records.blank? || (check_size && records.count != check_size) assoc.destroy(records) end define_method "#{_reflect_of(content).to_s.singularize}_names" do send("_#{content.to_s.pluralize}").map(&:name).map(&:to_sym) end define_singleton_method "#{_reflect_of(content).to_s.singularize}_names" do all.flat_map { |user| user.send("#{_reflect_of(content).to_s.singularize}_name") }.uniq end end end end |
#class_reflections ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/i_am_i_can/dynamic_generate.rb', line 17 def class_reflections # Extend each associated querying to a class method that returns ActiveRecord::Relation # # Suppose: in UserRole model, # has_and_belongs_to_many :related_users # # It will do like this: # def self.related_users # i_am_i_can.subject_model.with_stored_roles.where(user_roles: { id: self.ids }) # end # # Usage: # UserRole.all.related_users # proc do %w[ subject role role_group permission ].each do |k| next unless _reflect_of(k) define_singleton_method _reflect_of(k) do model = i_am_i_can.send("#{k}_model") raise NoMethodError unless (reflect_name = model._reflect_of(i_am_i_can.act)) model.send("with_#{reflect_name}").where( self.name.underscore.pluralize => { id: self.ids } ) end end end end |
#scopes ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/i_am_i_can/dynamic_generate.rb', line 5 def scopes # Generate scopes of each specified i_am_i_can association # # scope :with_stored_roles, -> { includes(:stored_roles) } # proc do |keys| keys.each do |k| scope :"with_#{_reflect_of(k)}", -> { includes(_reflect_of(k)) } end end end |