Class: Pinkman::Model::Base

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/pinkman/model/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get(query) ⇒ Object

Get: Pinkman way of fetching records



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pinkman/model/base.rb', line 19

def self.get query
  if (begin Integer(query) rescue false end)
    [find(query)]
  elsif query.is_a? Hash
    where(query)
  elsif query.is_a? Array
    where(id: query)
  elsif query.is_a? String
    search(query)
  else
    []
  end
end

.optim(*args) ⇒ Object



46
47
48
# File 'lib/pinkman/model/base.rb', line 46

def self.optim(*args)
  optimize(*args)
end

.optimize(scope, options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pinkman/model/base.rb', line 33

def self.optimize(scope, options = {})
  if  (options.keys - [:includes, :select]).any?
    raise ArgumentError, "#{self}.optimize called with invalid options. Options hash accepts :includes and :select only."
  end
  options_includes = options[:includes] || []
  options_select = options[:select] || []
  
  scope_obj = serializer.scope(scope)
  including_array = scope_obj.including + scope_obj.associations_inclusion + options_includes
  selecting_array = scope_obj.selecting + options_select
  
  includes(including_array).select(selecting_array)
end

.serializerObject



14
15
16
# File 'lib/pinkman/model/base.rb', line 14

def self.serializer
  @serializer || (begin eval(self.to_s + 'Serializer') rescue nil end)
end

.serializer=(value) ⇒ Object

Serializer



10
11
12
# File 'lib/pinkman/model/base.rb', line 10

def self.serializer= value
  @serializer = value
end

Instance Method Details

#has_json_key?(key, scope = :public, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/pinkman/model/base.rb', line 64

def has_json_key? key, scope=:public, options={}
  json_version(scope, options).has_key?(key.to_s) and json_version(scope, options)[key.to_s].present?
end

#json(scope_name = :public, params_hash = {}) ⇒ Object



55
56
57
# File 'lib/pinkman/model/base.rb', line 55

def json scope_name=:public, params_hash={}
  serialize_for(scope_name, params_hash).to_json
end

#json_for(*args, &block) ⇒ Object



59
60
61
62
# File 'lib/pinkman/model/base.rb', line 59

def json_for *args, &block
  ActiveSupport::Deprecation.warn('"json_for" deprecated. Use "json" instead.')
  json(*args, &block)
end

#json_key(key, scope = :public, options = {}) ⇒ Object



73
74
75
# File 'lib/pinkman/model/base.rb', line 73

def json_key key, scope=:public, options={}
  json_version(scope, options)[key.to_s]
end

#json_version(*args) ⇒ Object Also known as: json_hash



68
69
70
# File 'lib/pinkman/model/base.rb', line 68

def json_version *args
  JSON.parse(json(*args))
end

#serialize_for(scope_name, params_hash = {}) ⇒ Object



50
51
52
53
# File 'lib/pinkman/model/base.rb', line 50

def serialize_for scope_name, params_hash = {}
  options = {scope: scope_name}.merge(params: params_hash)
  self.class.serializer.new(self,options)
end