Class: BazaModels::Model
Defined Under Namespace
Modules: BelongsToRelations, CustomValidations, Delegation, HasManyRelations, HasOneRelations, Manipulation, Queries, Scopes, TranslationFunctionality, Validations
Constant Summary
collapse
- CALLBACK_TYPES =
Define all callback methods.
[
:after_initialize, :after_find,
:before_create, :after_create, :before_save, :after_save, :before_destroy, :after_destroy,
:before_validation, :after_validation, :before_validation_on_create, :after_validation_on_create,
:before_validation_on_update, :after_validation_on_update
]
- QUERY_METHODS =
[
:all, :any?, :empty?, :none?, :count, :find, :first, :find_first, :last, :length, :select, :includes,
:joins, :group, :where, :order, :limit, :to_a, :accessible_by
]
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
included, #valid?
included, #model_name
Methods included from Scopes
included
Methods included from Queries
included
#assign_attributes, #create, #create!, #destroy, #destroy!, included, #save, #save!, #update_attributes, #update_attributes!
included
included
included
Methods included from Delegation
included
included
Constructor Details
#initialize(data = {}, args = {}) ⇒ Model
Returns a new instance of Model.
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/baza_models/model.rb', line 67
def initialize(data = {}, args = {})
self.class.init_model unless self.class.model_initialized?
reset_errors
@changes = {}
if args[:init]
@data = self.class.__blank_attributes.merge(real_attributes(data))
else
@data = self.class.__blank_attributes.clone
@changes.merge!(real_attributes(data))
end
if @data[:id]
@new_record = false
else
@new_record = true
fire_callbacks(:after_initialize)
end
end
|
Class Attribute Details
.__blank_attributes ⇒ Object
Returns the value of attribute __blank_attributes.
263
264
265
|
# File 'lib/baza_models/model.rb', line 263
def __blank_attributes
@__blank_attributes
end
|
.db ⇒ Object
105
106
107
108
109
110
111
|
# File 'lib/baza_models/model.rb', line 105
def self.db
@db = nil if @db && @db.closed?
return @db if @db
@db ||= BazaModels.primary_db
raise "No Baza database has been configured" unless @db
@db
end
|
.table_name ⇒ Object
136
137
138
|
# File 'lib/baza_models/model.rb', line 136
def self.table_name
@table_name ||= "#{StringCases.camel_to_snake(name.gsub("::", ""))}s"
end
|
Instance Attribute Details
#changes ⇒ Object
Returns the value of attribute changes.
29
30
31
|
# File 'lib/baza_models/model.rb', line 29
def changes
@changes
end
|
#data ⇒ Object
Returns the value of attribute data.
28
29
30
|
# File 'lib/baza_models/model.rb', line 28
def data
@data
end
|
#db ⇒ Object
Returns the value of attribute db.
28
29
30
|
# File 'lib/baza_models/model.rb', line 28
def db
@db
end
|
#errors ⇒ Object
Returns the value of attribute errors.
29
30
31
|
# File 'lib/baza_models/model.rb', line 29
def errors
@errors
end
|
#table_name ⇒ Object
132
133
134
|
# File 'lib/baza_models/model.rb', line 132
def table_name
@table_name ||= self.class.table_name
end
|
Class Method Details
.column_names ⇒ Object
224
225
226
|
# File 'lib/baza_models/model.rb', line 224
def self.column_names
@column_names ||= __blank_attributes.keys.map(&:to_s)
end
|
.init_attribute_from_column(column) ⇒ Object
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
# File 'lib/baza_models/model.rb', line 272
def self.init_attribute_from_column(column)
column_name = column.name.to_sym
define_method(column_name) do
read_attribute(column_name)
end
define_method("#{column_name}_was") do
return @data.fetch(column_name)
end
define_method("#{column_name}=") do |new_value|
write_attribute(column_name, new_value)
end
define_method("#{column_name}?") do
!@data.fetch(column_name).to_s.strip.empty?
end
define_method("#{column_name}_changed?") do
if @changes.key?(column_name) && @changes.fetch(column_name) != @data.fetch(column_name)
true
else
false
end
end
end
|
.init_model ⇒ Object
148
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/baza_models/model.rb', line 148
def self.init_model
@table = db.tables[table_name]
@__blank_attributes ||= {}
@table.columns do |column|
init_attribute_from_column(column)
@__blank_attributes[column.name.to_sym] = nil
end
@model_initialized = true
end
|
.model_initialized? ⇒ Boolean
rubocop:disable Style/TrivialAccessors
267
268
269
270
|
# File 'lib/baza_models/model.rb', line 267
def self.model_initialized?
@model_initialized
end
|
.ransack(params) ⇒ Object
228
229
230
|
# File 'lib/baza_models/model.rb', line 228
def self.ransack(params)
BazaModels::Ransacker.new(class: self, params: params)
end
|
.relationships ⇒ Object
140
141
142
|
# File 'lib/baza_models/model.rb', line 140
def self.relationships
@relationships ||= {}
end
|
.transaction(&blk) ⇒ Object
117
118
119
|
# File 'lib/baza_models/model.rb', line 117
def self.transaction(&blk)
@db.transaction(&blk)
end
|
Instance Method Details
#==(other) ⇒ Object
208
209
210
211
212
213
214
215
216
|
# File 'lib/baza_models/model.rb', line 208
def ==(other)
return false unless self.class == other.class
if new_record? && other.new_record?
return merged_data == other.__send__(:merged_data)
else
return id == other.id
end
end
|
#[](key) ⇒ Object
232
233
234
|
# File 'lib/baza_models/model.rb', line 232
def [](key)
read_attribute(key)
end
|
#[]=(key, value) ⇒ Object
236
237
238
|
# File 'lib/baza_models/model.rb', line 236
def []=(key, value)
write_attribute(key, value)
end
|
#autoloads ⇒ Object
127
128
129
130
|
# File 'lib/baza_models/model.rb', line 127
def autoloads
@autoloads ||= {}
@autoloads
end
|
#changed? ⇒ Boolean
249
250
251
252
253
254
255
256
257
258
|
# File 'lib/baza_models/model.rb', line 249
def changed?
changed = false
@changes.each do |key, value|
next if @data.fetch(key) == value
changed = true
break
end
changed
end
|
#has_attribute?(name) ⇒ Boolean
rubocop:disable Style/PredicateName
219
220
221
222
|
# File 'lib/baza_models/model.rb', line 219
def has_attribute?(name)
self.class.column_names.include?(name.to_s)
end
|
#id ⇒ Object
161
162
163
|
# File 'lib/baza_models/model.rb', line 161
def id
@data.fetch(:id)
end
|
#inspect ⇒ Object
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
# File 'lib/baza_models/model.rb', line 192
def inspect
data_str = ""
@data.each do |key, value|
if @changes.key?(key)
value_to_use = @changes.fetch(key)
else
value_to_use = value
end
data_str << " " unless data_str.empty?
data_str << "#{key}=\"#{value_to_use}\""
end
"#<#{self.class.name} #{data_str}>"
end
|
#new_record? ⇒ Boolean
rubocop:disable Style/TrivialAccessors
89
90
91
92
|
# File 'lib/baza_models/model.rb', line 89
def new_record?
@new_record
end
|
#persisted? ⇒ Boolean
94
95
96
|
# File 'lib/baza_models/model.rb', line 94
def persisted?
!new_record?
end
|
#read_attribute(attribute_name) ⇒ Object
240
241
242
243
|
# File 'lib/baza_models/model.rb', line 240
def read_attribute(attribute_name)
return @changes.fetch(attribute_name) if @changes.key?(attribute_name)
@data.fetch(attribute_name)
end
|
#reload ⇒ Object
177
178
179
180
181
182
|
# File 'lib/baza_models/model.rb', line 177
def reload
@data = db.single(table_name, {id: id}, limit: 1)
raise BazaModels::Errors::RecordNotFound unless @data
@changes = {}
self
end
|
#to_key ⇒ Object
169
170
171
172
173
174
175
|
# File 'lib/baza_models/model.rb', line 169
def to_key
if new_record?
nil
else
[id]
end
end
|
#to_model ⇒ Object
144
145
146
|
# File 'lib/baza_models/model.rb', line 144
def to_model
self
end
|
#to_param ⇒ Object
165
166
167
|
# File 'lib/baza_models/model.rb', line 165
def to_param
id.to_s if id
end
|
#to_s ⇒ Object
184
185
186
187
188
189
190
|
# File 'lib/baza_models/model.rb', line 184
def to_s
if new_record?
"#<#{self.class.name} new!>"
else
"#<#{self.class.name} id=#{id}>"
end
end
|
#write_attribute(attribute_name, value) ⇒ Object
245
246
247
|
# File 'lib/baza_models/model.rb', line 245
def write_attribute(attribute_name, value)
@changes[attribute_name] = value
end
|