Class: Her::Model::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/her_extension/model/relation.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ Relation

Returns a new instance of Relation.



10
11
12
13
# File 'lib/her_extension/model/relation.rb', line 10

def initialize(parent)
  @parent = parent
  @params = {}
end

Class Method Details

.scopesObject

Hold the scopes defined on Her::Model classes E.g.: scope :admin, -> { where(admin: 1) }



95
96
97
# File 'lib/her_extension/model/relation.rb', line 95

def self.scopes
  @scopes ||= {}
end

Instance Method Details

#fetchObject

Fetch a collection of resources



16
17
18
19
20
21
22
# File 'lib/her_extension/model/relation.rb', line 16

def fetch
  path = @parent.build_request_path(@params)
  method = @parent.method_for(:find)
  @parent.request(@params.merge(:_method => method, :_path => path)) do |parsed_data, response|
    @parent.new_collection(parsed_data)
  end
end

#first_or_create(attributes = {}) ⇒ Object

Patch to unwrap filter when creating



39
40
41
# File 'lib/her_extension/model/relation.rb', line 39

def first_or_create(attributes = {})
  fetch.first || create((@params.delete(:filter) || {}).merge(attributes))
end

#first_or_initialize(attributes = {}) ⇒ Object

Patch to unwrap filter when creating



44
45
46
# File 'lib/her_extension/model/relation.rb', line 44

def first_or_initialize(attributes = {})
  fetch.first || build((@params.delete(:filter) || {}).merge(attributes))
end

#limit(max = nil) ⇒ Object

Limit the number of results returned



70
71
72
73
74
# File 'lib/her_extension/model/relation.rb', line 70

def limit(max = nil)
  return self if !max
  self.params[:limit] = max
  self
end

#order(string_query) ⇒ Object Also known as: sort

ActiveRecord-like order Product.order(“created_at DESC, name ASC”)



59
60
61
62
63
64
65
66
# File 'lib/her_extension/model/relation.rb', line 59

def order(string_query)
  return self if !string_query || string_query.empty?
  args = string_query.split(',').map do |q|
    field, direction = q.strip.split(/\s+/).compact
    [field, direction ? direction.downcase : nil].join('.')
  end
  self.order_by(*args)
end

#order_by(*args) ⇒ Object Also known as: sort_by

E.g: Product.order_by(‘created_at.desc’,‘name.asc’)



50
51
52
53
54
# File 'lib/her_extension/model/relation.rb', line 50

def order_by(*args)
  return self if args.empty?
  self.params[:sort] = [self.params[:sort],args].flatten.compact.uniq
  self
end

#reloadObject

Refetch the relation



89
90
91
# File 'lib/her_extension/model/relation.rb', line 89

def reload
  fetch
end

#reset_paramsObject

Reset the query parameters



84
85
86
# File 'lib/her_extension/model/relation.rb', line 84

def reset_params
  @params.clear
end

#skip(nrows = nil) ⇒ Object

Skip result rows



77
78
79
80
81
# File 'lib/her_extension/model/relation.rb', line 77

def skip(nrows = nil)
  return self if !nrows
  self.params[:skip] = nrows
  self
end

#where(params = {}) ⇒ Object Also known as: all

Override Her::Model::Relation#where to follow jsonapi.org standards Use filter instead of raw parameters



27
28
29
30
31
32
33
34
35
# File 'lib/her_extension/model/relation.rb', line 27

def where(params = {})
  return self if !params || params.empty?
  # If a value is an empty array, it'll be excluded when calling params.to_query, so convert it to nil instead
  params.each { |k, v| params[k] = v.presence if v.is_a?(Array) }

  self.params[:filter] ||= {}
  self.params[:filter].merge!(params)
  self
end