Module: Oare::Resource::ClassMethods

Defined in:
lib/active_resource_override/base.rb

Overview

Instance Methods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



116
117
118
# File 'lib/active_resource_override/base.rb', line 116

def access_token
  @access_token
end

#associationsObject

Returns the value of attribute associations.



117
118
119
# File 'lib/active_resource_override/base.rb', line 117

def associations
  @associations
end

#nested_attributesObject

Returns the value of attribute nested_attributes.



118
119
120
# File 'lib/active_resource_override/base.rb', line 118

def nested_attributes
  @nested_attributes
end

Instance Method Details

#accepts_nested_attributes_for(association_id, options = {}) ⇒ Object



146
147
148
149
150
151
152
153
154
155
# File 'lib/active_resource_override/base.rb', line 146

def accepts_nested_attributes_for(association_id, options = {})
  nested_attributes["#{association_id}_attributes"] = association_id
  define_method("#{association_id}_attributes=") do |*args|
    # TODO
  end

  define_method("#{association_id}_attributes") do
    nested_attributes_values["#{association_id}_attributes"]
  end
end

#connection(refresh = true) ⇒ Object



162
163
164
# File 'lib/active_resource_override/base.rb', line 162

def connection(refresh = true)
  self.access_token ? self.oauth_connection : super
end

#create_path(path_options = {}, query_options = nil) ⇒ Object



176
177
178
179
180
181
# File 'lib/active_resource_override/base.rb', line 176

def create_path(path_options = {}, query_options = nil)
  path = path_options.delete(:path)
  prefix_options = path_options
  return collection_path(prefix_options, query_options) unless path
  "/#{path}.#{format.extension}#{query_string(query_options)}"
end

#has_many(association_id, options = {}) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/active_resource_override/base.rb', line 125

def has_many(association_id, options = {})
  class_name = options[:class_name] || association_id.to_s.singularize.camelize
  associations << class_name.constantize

  define_method(association_id) do |*args|
    current_value = instance_variable_get("@#{association_id}".to_sym)
    return current_value if current_value

    resource = find_or_create_resource_for_collection(class_name)
    value = if self.new_record? then [resource.new]
      else
      # TODO:
      # Request for users of account
      # Use an instance variable version of the access token
      #
      end
    instance_variable_set(
      "@#{association_id}".to_sym, value)
  end
end

#oauth_connection(refresh = true) ⇒ Object



166
167
168
169
170
171
172
173
174
# File 'lib/active_resource_override/base.rb', line 166

def oauth_connection(refresh = true)
  associations.each do |model_constant|
    model_constant.access_token = self.access_token
  end if associations

  @connection = Oare::Connection.new(self.access_token) if @connection.nil? || refresh
  @connection.timeout = timeout if timeout
  @connection
end

#set_default_valuesObject



120
121
122
123
# File 'lib/active_resource_override/base.rb', line 120

def set_default_values
  self.nested_attributes ||= {}
  self.associations      ||= []
end

#update_path(id, path_options = {}, query_options = nil) ⇒ Object



183
184
185
186
187
188
# File 'lib/active_resource_override/base.rb', line 183

def update_path(id, path_options = {}, query_options = nil)
  path = path_options.delete(:path)
  prefix_options = path_options
  return element_path(id, prefix_options, query_options) unless path
  "/#{path}.#{format.extension}#{query_string(query_options)}"
end