Module: RestPack::Resource::ClassMethods
- Defined in:
- lib/restpack-resource/resource.rb
Instance Method Summary collapse
- #association_relationships(association) ⇒ Object
- #extract_includes_array_from_params(params) ⇒ Object
- #get_child_models(relationship, foreign_keys, page_size) ⇒ Object
- #get_one_to_many_side_loads(relationship, foreign_keys) ⇒ Object
- #invalid_include(relationship) ⇒ Object
- #model_as_resource(model) ⇒ Object
Instance Method Details
#association_relationships(association) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/restpack-resource/resource.rb', line 29 def association_relationships(association) target_model_name = association.to_s.singularize.capitalize relationships = self.relationships.select {|r| r.target_model.to_s == target_model_name } raise InvalidInclude if relationships.empty? relationships end |
#extract_includes_array_from_params(params) ⇒ Object
50 51 52 |
# File 'lib/restpack-resource/resource.rb', line 50 def extract_includes_array_from_params(params) params[:includes].nil? ? [] : params[:includes].split(',') end |
#get_child_models(relationship, foreign_keys, page_size) ⇒ Object
36 37 38 39 |
# File 'lib/restpack-resource/resource.rb', line 36 def get_child_models(relationship, foreign_keys, page_size) child_key_name = relationship.child_key.first.name relationship.child_model.all(child_key_name.to_sym => foreign_keys).page({ per_page: page_size }) end |
#get_one_to_many_side_loads(relationship, foreign_keys) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/restpack-resource/resource.rb', line 41 def get_one_to_many_side_loads(relationship, foreign_keys) result = {} children = get_child_models(relationship, foreign_keys, 100) #TODO: GJ: configurable side-load page size result[:resources] = children.map {|c| c.as_resource() } result[:count_key] = "#{relationship.child_model_name.downcase}_count".to_sym result[:count] = children.pager.total result end |
#invalid_include(relationship) ⇒ Object
54 55 56 |
# File 'lib/restpack-resource/resource.rb', line 54 def invalid_include(relationship) raise InvalidInclude, "#{self.name}.#{relationship.name} can't be included when paging #{self.name.pluralize.downcase}" end |
#model_as_resource(model) ⇒ Object
25 26 27 |
# File 'lib/restpack-resource/resource.rb', line 25 def model_as_resource(model) model ? model.as_resource() : nil end |