Module: JSONAPI::Consumer::Resource::AssociationConcern::ClassMethods
- Defined in:
- lib/jsonapi/consumer/resource/association_concern.rb
Instance Attribute Summary collapse
-
#_associations ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#_association_class_name(name) ⇒ Object
:nodoc:.
-
#_association_for(name) ⇒ Object
:nodoc:.
-
#_association_type(name) ⇒ Object
:nodoc:.
-
#associate(type, attrs) ⇒ Object
:nodoc:.
- #belongs_to(*attrs) ⇒ Object
-
#has_many(*attrs) ⇒ Object
Defines a has many relationship.
-
#has_one(*attrs) ⇒ Object
Defines a single relationship.
Instance Attribute Details
#_associations ⇒ Object
:nodoc:
39 40 41 |
# File 'lib/jsonapi/consumer/resource/association_concern.rb', line 39 def _associations @_associations ||= {} end |
Instance Method Details
#_association_class_name(name) ⇒ Object
:nodoc:
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/jsonapi/consumer/resource/association_concern.rb', line 54 def _association_class_name(name) if class_name = _association_for(name).fetch(:class_name) begin class_name.constantize rescue NameError raise MisconfiguredAssociation, "#{self}##{_association_type(name)} #{name} has a class_name specified that does not exist." end else raise MisconfiguredAssociation, "#{self}##{_association_type(name)} #{name} is missing an explicit `:class_name` value." end end |
#_association_for(name) ⇒ Object
:nodoc:
44 45 46 |
# File 'lib/jsonapi/consumer/resource/association_concern.rb', line 44 def _association_for(name) _associations[name.to_sym] end |
#_association_type(name) ⇒ Object
:nodoc:
49 50 51 |
# File 'lib/jsonapi/consumer/resource/association_concern.rb', line 49 def _association_type(name) _association_for(name).fetch(:type) end |
#associate(type, attrs) ⇒ Object
:nodoc:
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/jsonapi/consumer/resource/association_concern.rb', line 69 def associate(type, attrs) = attrs. self._associations = _associations.dup attrs.each do |attr| unless method_defined?(attr) define_method attr do read_association(attr) end end if type == :has_many unless method_defined?(:"#{attr.to_s.singularize}_ids") define_method :"#{attr.to_s.singularize}_ids" do if objs = read_association(attr) objs.collect {|o| o.send(o.primary_key)} end end end else unless method_defined?(:"#{attr.to_s.singularize}_id") define_method :"#{attr.to_s.singularize}_id" do if obj = read_association(attr) obj.send(obj.primary_key) end end end end unless method_defined?(:"#{attr}=") define_method :"#{attr}=" do |val| val = [val].flatten if type == :has_many && !val.nil? set_association(attr, val) end end self._associations[attr] = {type: type, class_name: .delete(:class_name), options: } end end |
#belongs_to(*attrs) ⇒ Object
TODO:
belongs to is not supported yet.
23 24 25 |
# File 'lib/jsonapi/consumer/resource/association_concern.rb', line 23 def belongs_to(*attrs) associate(:belongs_to, attrs) end |
#has_many(*attrs) ⇒ Object
Defines a has many relationship.
17 18 19 |
# File 'lib/jsonapi/consumer/resource/association_concern.rb', line 17 def has_many(*attrs) associate(:has_many, attrs) end |
#has_one(*attrs) ⇒ Object
Defines a single relationship.
34 35 36 |
# File 'lib/jsonapi/consumer/resource/association_concern.rb', line 34 def has_one(*attrs) associate(:has_one, attrs) end |