class Her::Model::Relation

Public Class Methods

new(parent) click to toggle source
# File lib/her_extension/model/relation.rb, line 10
def initialize(parent)
  @parent = parent
  @params = {}
end
scopes() click to toggle source

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

# File lib/her_extension/model/relation.rb, line 95
def self.scopes
  @scopes ||= {}
end

Public Instance Methods

all(params = {})
Alias for: where
fetch() click to toggle source

Fetch a collection of resources

# 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 = {}) click to toggle source

Patch to unwrap filter when creating

# 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 = {}) click to toggle source

Patch to unwrap filter when creating

# 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) click to toggle source

Limit the number of results returned

# 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) click to toggle source

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

# 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
Also aliased as: sort
order_by(*args) click to toggle source

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

# 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
Also aliased as: sort_by
reload() click to toggle source

Refetch the relation

# File lib/her_extension/model/relation.rb, line 89
def reload
  fetch
end
reset_params() click to toggle source

Reset the query parameters

# File lib/her_extension/model/relation.rb, line 84
def reset_params
  @params.clear
end
skip(nrows = nil) click to toggle source

Skip result rows

# File lib/her_extension/model/relation.rb, line 77
def skip(nrows = nil)
  return self if !nrows
  self.params[:skip] = nrows
  self
end
sort(string_query)
Alias for: order
sort_by(*args)
Alias for: order_by
where(params = {}) click to toggle source

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

# 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
Also aliased as: all

Private Instance Methods

query_checkum() click to toggle source
# File lib/her_extension/model/relation.rb, line 100
def query_checkum
  Digest::MD5.hexdigest(Marshal.dump(@params))
end