Class: NinjaModel::Base
- Inherits:
-
Object
show all
- Extended by:
- ActiveModel::Naming, ActiveModel::Translation
- Includes:
- ActiveModel::Observing, ActiveModel::Serializers::JSON, ActiveModel::Serializers::Xml, ActiveRecord::Aggregations, ActiveRecord::NamedScope, Adapters, Associations, AttributeMethods, Callbacks, Identity, Marshalling, Persistence, Reflection, Validation
- Defined in:
- lib/ninja_model/base.rb
Instance Attribute Summary
Attributes included from Associations
#association_cache
Class Method Summary
collapse
Instance Method Summary
collapse
#association
Methods included from Adapters
#adapter
Methods included from Validation
#save, #valid?
Methods included from Identity
#to_key, #to_model, #to_param
#[], #[]=, #attributes=, #attributes_from_model_attributes, #read_attribute, #write_attribute
Constructor Details
#initialize(attributes = nil, options = {}) {|_self| ... } ⇒ Base
Returns a new instance of Base.
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/ninja_model/base.rb', line 124
def initialize(attributes = nil, options = {})
@attributes = attributes_from_model_attributes
@association_cache = {}
@aggregation_cache = {}
@persisted = false
@readonly = true
@destroyed = false
populate_with_current_scope_attributes
self.attributes = attributes unless attributes.nil?
yield self if block_given?
run_callbacks :initialize
end
|
Class Method Details
.build_finder_relation(options = {}, scope = nil) ⇒ Object
72
73
74
75
76
|
# File 'lib/ninja_model/base.rb', line 72
def build_finder_relation(options = {}, scope = nil)
relation = options.is_a?(Hash) ? unscoped.apply_finder_options(options) : options
relation = scope.merge(relation) if scope
relation
end
|
.compute_type(type_name) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/ninja_model/base.rb', line 78
def compute_type(type_name)
if type_name.match(/^::/)
ActiveSupport::Dependencies.constantize(type_name)
else
candidates = []
name.scan(/::|$/) { candidates.unshift "#{$`}::#{type_name}" }
candidates << type_name
candidates.each do |candidate|
begin
constant = ActiveSupport::Dependencies.constantize(candidate)
return constant if candidate == constant.to_s
rescue NameError => e
raise e unless e.instance_of?(NameError)
end
end
raise NameError, "uninitialized constant #{candidates.first}"
end
end
|
.current_scope ⇒ Object
58
59
60
|
# File 'lib/ninja_model/base.rb', line 58
def current_scope
current_scoped_methods
end
|
.current_scoped_methods ⇒ Object
62
63
64
65
|
# File 'lib/ninja_model/base.rb', line 62
def current_scoped_methods
last = scoped_methods.last
last.is_a?(Proc) ? unscoped(&last) : last
end
|
.default_scope(scope = {}) ⇒ Object
53
54
55
56
|
# File 'lib/ninja_model/base.rb', line 53
def default_scope(scope = {})
scope = Proc.new if block_given?
self.default_scopes = default_scopes + [scope]
end
|
.logger ⇒ Object
40
41
42
|
# File 'lib/ninja_model/base.rb', line 40
def logger
::NinjaModel.logger
end
|
.relation ⇒ Object
36
37
38
|
# File 'lib/ninja_model/base.rb', line 36
def relation
@relation ||= Relation.new(self)
end
|
.reset_scoped_methods ⇒ Object
67
68
69
|
# File 'lib/ninja_model/base.rb', line 67
def reset_scoped_methods
Thread.current["#{self}_scoped_methods".to_sym] = nil
end
|
.scoped_methods ⇒ Object
48
49
50
51
|
# File 'lib/ninja_model/base.rb', line 48
def scoped_methods
key = "#{self}_scoped_methods".to_sym
Thread.current[key] = Thread.current[key].presence || self.default_scopes.dup
end
|
.unscoped ⇒ Object
44
45
46
|
# File 'lib/ninja_model/base.rb', line 44
def unscoped
block_given? ? relation.scoping { yield } : relation
end
|
Instance Method Details
#assign_attributes(new_attributes, options = {}) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/ninja_model/base.rb', line 104
def assign_attributes(new_attributes, options = {})
return unless new_attributes
attributes = new_attributes.stringify_keys
attributes.each do |k, v|
if respond_to?("#{k}=")
send("#{k}=", v)
else
raise(StandardError, "unknown attribute: #{k}")
end
end
end
|
#attribute_for_inspect(attr_name) ⇒ Object
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/ninja_model/base.rb', line 156
def attribute_for_inspect(attr_name)
value = read_attribute(attr_name)
if value.is_a?(String) && value.length > 50
"#{value[0..50]}...".inspect
elsif value.is_a?(Date) || value.is_a?(Time)
%("#{value.to_s(:db)}")
else
value.inspect
end
end
|
#attributes ⇒ Object
118
119
120
121
122
|
# File 'lib/ninja_model/base.rb', line 118
def attributes
self.class.attribute_names.inject({}) { |h, v|
h[v] = read_attribute(v); h
}
end
|
#derive_class(association_id) ⇒ Object
150
151
152
153
154
|
# File 'lib/ninja_model/base.rb', line 150
def derive_class(association_id)
klass = association_id.to_s.camelize
klass = klass.singularize
compute_type(klass)
end
|
#inspect ⇒ Object
167
168
169
170
171
172
|
# File 'lib/ninja_model/base.rb', line 167
def inspect
attributes_as_nice_string = self.class.attribute_names.collect { |attr|
"#{attr}: #{attribute_for_inspect(attr)}"
}.compact.join(', ')
"#<#{self.class} #{attributes_as_nice_string}>"
end
|
#instantiate(record) ⇒ Object
140
141
142
143
144
145
146
147
148
|
# File 'lib/ninja_model/base.rb', line 140
def instantiate(record)
@attributes = record.stringify_keys
@readonly = @destroyed = false
@persisted = true
_run_find_callbacks
_run_initialize_callbacks
self
end
|