Module: ActiveShotgun::Model::Associations::ClassMethods
- Defined in:
- lib/active_shotgun/model/associations.rb
Instance Method Summary collapse
- #belongs_to(assoc_name, klass: nil, types: nil, type: nil) ⇒ Object
- #has_many(assoc_name_plural, possible_types: nil) ⇒ Object
Instance Method Details
#belongs_to(assoc_name, klass: nil, types: nil, type: nil) ⇒ Object
12 13 14 15 16 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 44 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 |
# File 'lib/active_shotgun/model/associations.rb', line 12 def belongs_to(assoc_name, klass: nil, types: nil, type: nil) klass ||= assoc_name.to_s.camelize types ||= [type].flatten if type types ||= [assoc_name.to_s.camelize] types = [types].flatten.map(&:to_s) # define name_id # define name that read and populate the class instance_eval do # Register the association self::BELONG_ASSOC.push(assoc_name) # Define the id reader attr_reader "#{assoc_name}_id" # Define the id writter define_attribute_methods("#{assoc_name}_id") define_method("#{assoc_name}_id=") do |value, new_type = nil| send("#{assoc_name}_id_will_change!") if new_type unless types.include?(new_type) raise "Invalid Type #{new_type}. Valid types are: [#{types.join(', ')}]" end instance_variable_set("@#{assoc_name}_type", new_type) elsif types.size == 1 instance_variable_set("@#{assoc_name}_type", types.first) else unless public_send("#{assoc_name}_type") raise "Multiple types possible. You must specify a type from [#{types.join(', ')}]" end end instance_variable_set("@#{assoc_name}_id", value) end # Define the type reader attr_reader "#{assoc_name}_type" # Define the assoc reader define_method(assoc_name) do instance_variable_get("@#{assoc_name}") || instance_variable_set( "@#{assoc_name}", (klass.is_a?(String) ? klass.constantize : klass). parse_shotgun_results( Client. shotgun. entities(public_send("#{assoc_name}_type")). find(public_send("#{assoc_name}_id")) ) ) end define_method("#{assoc_name}=") do |assoc_item| public_send("#{assoc_name}_id=", assoc_item.id, assoc_item.class.shotgun_type) instance_variable_set("@#{assoc_name}", assoc_item) end end end |
#has_many(assoc_name_plural, possible_types: nil) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/active_shotgun/model/associations.rb', line 71 def has_many(assoc_name_plural, possible_types: nil) assoc_name = assoc_name_plural.to_s.singularize possible_types ||= { assoc_name.camelize => assoc_name.camelize, } if possible_types.is_a?(Array) possible_types.map do |type| [ type.camelize, type.camelize, ] end.to_h end # define name that read and populate an array of (a query ?) instance_eval do # Register the association self::MANY_ASSOC.push(assoc_name_plural) # Define reader must return an AssociationQuery which override push/<</delete with instant remove/add define_method(assoc_name_plural) do instance_variable_get("@#{assoc_name_plural}") || instance_variable_set( "@#{assoc_name_plural}", AssociationsProxy.new( possible_types: possible_types, base_class: self.class, base_id: id, field_name: assoc_name_plural ).where( [ self.class.shotgun_type.downcase.to_s.pluralize, "is", { type: self.class.shotgun_type.camelize, id: id, }, ] ) ) end # Define writer define_attribute_methods(assoc_name_plural) define_method("#{assoc_name_plural}=") do |array| send("#{assoc_name_plural}_will_change!") instance_variable_set("@#{assoc_name_plural}", array) end end end |