Class: Her::Model::Associations::HasManyAssociation

Inherits:
Association
  • Object
show all
Defined in:
lib/her_extension/model/associations/has_many_association.rb

Instance Attribute Summary

Attributes inherited from Association

#params

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Association

#blank_relation, #build_request_path, #method_for, #method_missing, #new_collection, #reload, #request, #to_params

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Her::Model::Associations::Association

Class Method Details

.attach(klass, name, opts) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/her_extension/model/associations/has_many_association.rb', line 11

def self.attach(klass, name, opts)
  opts = {
    :class_name     => name.to_s.classify,
    :name           => name,
    :data_key       => name,
    :default        => Her::Collection.new,
    :path           => "/#{name}",
    :inverse_of => nil
  }.merge(opts)
  klass.associations[:has_many] << opts

  klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{name}
      cached_name = :"@_her_association_#{name}"
      cached_data = (instance_variable_defined?(cached_name) && instance_variable_get(cached_name))
      opts = Marshal.load(#{Marshal.dump(opts).inspect})
      cached_data || instance_variable_set(cached_name, Her::Model::Associations::HasManyAssociation.proxy(self, opts))
    end
  RUBY
end

Instance Method Details

#build(attributes = {}) ⇒ Object

Initialize a new object with a foreign key to the parent

Examples:

class User
  include Her::Model
  has_many :comments
end

class Comment
  include Her::Model
end

user = User.find(1)
new_comment = user.comments.build(:body => "Hello!")
new_comment # => #<Comment body="Hello!">


48
49
50
# File 'lib/her_extension/model/associations/has_many_association.rb', line 48

def build(attributes = {})
  @klass.build(attributes)
end

#create(attributes = {}) ⇒ Object

Post an object to the nested resource collection endpoint then refetch the nested collection

Examples:

class User
  include Her::Model
  has_many :comments
end

class Comment
  include Her::Model
end

user = User.find(1)
user.comments.create(:body => "Hello!")
user.comments # => [#<Comment id=2 user_id=1 body="Hello!">]


68
69
70
71
# File 'lib/her_extension/model/associations/has_many_association.rb', line 68

def create(attributes = {})
  resp = self.execute_request(:create,attributes)
  @klass.build(resp)
end

#destroy(attributes = {}) ⇒ Object

Consider removing - not sure this method on a has_many collection has any meaning



79
80
81
# File 'lib/her_extension/model/associations/has_many_association.rb', line 79

def destroy(attributes = {})
  self.execute_request(:destroy,attributes)
end

#execute_request(action, attrs) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/her_extension/model/associations/has_many_association.rb', line 83

def execute_request(action, attrs)
  attributes = HashWithIndifferentAccess.new(attrs)

  # Post data to the collection endpoint
  resource = nil
  path = self.build_request_path
  method = self.method_for(action.to_sym)

  # Add ID to path if resource method
  if [:put, :patch, :delete].include?(method.to_sym)
    path += "/#{attributes[@klass.primary_key]}"
  end

  params = self.to_params(attributes).merge(:_method => method, :_path => path)
  self.request(params) do |parsed_data, response|
    resource = parsed_data if response.success?
  end

  # Reload nested collection
  self.reload if resource

  resource
end

#fetchObject

Override fetch to not do any smart stuff…



108
109
110
# File 'lib/her_extension/model/associations/has_many_association.rb', line 108

def fetch
  super
end

#update(attributes = {}) ⇒ Object

Consider removing - not sure this method on a has_many collection has any meaning



74
75
76
# File 'lib/her_extension/model/associations/has_many_association.rb', line 74

def update(attributes = {})
  self.execute_request(:update,attributes)
end