Class: Pinkman::Model::Base
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Pinkman::Model::Base
- Defined in:
- lib/pinkman/model/base.rb
Class Method Summary collapse
-
.get(query) ⇒ Object
Get: Pinkman way of fetching records.
- .optim(*args) ⇒ Object
- .optimize(scope, options = {}) ⇒ Object
- .serializer ⇒ Object
-
.serializer=(value) ⇒ Object
Serializer.
Instance Method Summary collapse
- #has_json_key?(key, scope = :public, options = {}) ⇒ Boolean
- #json(scope_name = :public, params_hash = {}) ⇒ Object
- #json_for(*args, &block) ⇒ Object
- #json_key(key, scope = :public, options = {}) ⇒ Object
- #json_version(*args) ⇒ Object (also: #json_hash)
- #serialize_for(scope_name, params_hash = {}) ⇒ Object
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, = {}) if (.keys - [:includes, :select]).any? raise ArgumentError, "#{self}.optimize called with invalid options. Options hash accepts :includes and :select only." end = [:includes] || [] = [:select] || [] scope_obj = serializer.scope(scope) including_array = scope_obj.including + scope_obj.associations_inclusion + selecting_array = scope_obj.selecting + includes(including_array).select(selecting_array) end |
.serializer ⇒ Object
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
64 65 66 |
# File 'lib/pinkman/model/base.rb', line 64 def has_json_key? key, scope=:public, ={} json_version(scope, ).has_key?(key.to_s) and json_version(scope, )[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, ={} json_version(scope, )[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 = {} = {scope: scope_name}.merge(params: params_hash) self.class.serializer.new(self,) end |