Class: Anoubis::Core::ApplicationRecord

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/anoubis/core/application_record.rb

Overview

Default ApplicationRecord for Anoubis::Core library.

Instance Attribute Summary collapse

Block of UUID functions collapse

Block of Redis functions collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#can_delete(args = {}) ⇒ Boolean (readonly)

Returns the ability to delete a data. By default all items may be deleted. For another result procedure should be overridden.

Returns:

  • (Boolean)

    true if data may be deleted



34
35
36
# File 'app/models/anoubis/core/application_record.rb', line 34

def can_delete
  @can_delete
end

#can_edit(args = {}) ⇒ Boolean (readonly)

Returns the ability to edit the data. By default all items may be edited. For another result procedure should be overridden.

Returns:

  • (Boolean)

    true if data may be edited



31
32
33
# File 'app/models/anoubis/core/application_record.rb', line 31

def can_edit
  @can_edit
end

#can_new(args = {}) ⇒ Boolean (readonly)

Returns the ability to create new data. By default all items may be deleted. For another result procedure should be overridden.

Returns:

  • (Boolean)

    true if new data may be created.



28
29
30
# File 'app/models/anoubis/core/application_record.rb', line 28

def can_new
  @can_new
end

#created_atDateTime

Returns the date and time when item had been created.

Returns:

  • (DateTime)

    the date and time when item had been created



# File 'app/models/anoubis/core/application_record.rb', line 6

#current_userString

Returns definition of current user for this record.

Returns:

  • (String)

    definition of current user for this record



18
19
20
# File 'app/models/anoubis/core/application_record.rb', line 18

def current_user
  @current_user
end

#need_refreshBoolean

Returns defines when table representation data should be updated even after simple update.

Returns:

  • (Boolean)

    defines when table representation data should be updated even after simple update



22
# File 'app/models/anoubis/core/application_record.rb', line 22

class_attribute :need_refresh, default: false

#redisObject

Returns pointer to Redis database.

Returns:

  • (Object)

    pointer to Redis database



14
# File 'app/models/anoubis/core/application_record.rb', line 14

class_attribute :redis

#sys_titleString (readonly)

Returns model’s system title. Default value is the row ID. For another result procedure should be overridden.

Returns:

  • (String)

    model’s system title



25
26
27
# File 'app/models/anoubis/core/application_record.rb', line 25

def sys_title
  @sys_title
end

#updated_atDateTime

Returns the date and time when item had been updated.

Returns:

  • (DateTime)

    the date and time when item had been updated



# File 'app/models/anoubis/core/application_record.rb', line 9

Class Method Details

.get_where(object, pid = 0) ⇒ Hash

Returns the default ActiveRecord ‘where’ for defined model.

Parameters:

  • object (ApplicationController)

    pointer to used Application controller

  • pid (Integer) (defaults to: 0)

    parent model id if present (default: 0). Variable doesn’t necessary

Returns:

  • (Hash)

    ActiveRecord ‘where’ definition



71
72
73
# File 'app/models/anoubis/core/application_record.rb', line 71

def self.get_where(object, pid = 0)
  { }
end

.redisObject

Returns reference to Redis database



203
204
205
# File 'app/models/anoubis/core/application_record.rb', line 203

def self.redis
  Redis.new
end

.redis_prefixObject

Returns defined application prefix for redis cache for model. Default value ”



192
193
194
195
196
197
198
199
# File 'app/models/anoubis/core/application_record.rb', line 192

def self.redis_prefix
  begin
    value = Rails.configuration.redis_prefix
  rescue
    return ''
  end
  return value + ':'
end

Instance Method Details

#after_initialize_core_anubis_modelObject

Is called after initialization Anoubis::Core ActiveRecord. Sets default parameters.



43
44
45
46
47
# File 'app/models/anoubis/core/application_record.rb', line 43

def after_initialize_core_anubis_model
  self.need_refresh = false
  self.redis = Redis.new
  self.current_user = nil
end

#can_destroy?Boolean

Checks if this record may be destroyed.

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
# File 'app/models/anoubis/core/application_record.rb', line 57

def can_destroy?
  result = true
  self.class.reflect_on_all_associations.all? do |assoc|
    result = self.send(assoc.name).nil? if assoc.macro == :has_one
    result = self.send(assoc.name).empty? if (assoc.macro == :has_many) && result
  end
  result
end

#current_localeObject

Return defined locale according by I18n



51
52
53
# File 'app/models/anoubis/core/application_record.rb', line 51

def current_locale
  I18n.locale.to_s
end

#current_locale=(value) ⇒ Object

Sets current locale and nullifies locale variable that presents model translation data.

Parameters:

  • value (String)

    new locale value (‘ru’, ‘en’, etc)



109
110
111
112
# File 'app/models/anoubis/core/application_record.rb', line 109

def current_locale=(value)
  @current_locale = value
  @model_locale = nil
end

#default_localeObject



217
218
219
# File 'app/models/anoubis/core/application_record.rb', line 217

def default_locale
  Rails.configuration.i18n.default_locale.to_s
end

#get_localeObject



209
210
211
212
213
214
215
# File 'app/models/anoubis/core/application_record.rb', line 209

def get_locale
  if self.current_locale && self.current_locale != ''
    return self.current_locale
  end

  self.default_locale
end

#get_locale_field(field, used_locale = nil) ⇒ Object



221
222
223
224
225
226
227
228
229
230
# File 'app/models/anoubis/core/application_record.rb', line 221

def get_locale_field(field, used_locale = nil)
  field = field.to_s.to_sym
  used_locale = self.current_locale.to_s unless used_locale

  return '' unless self[field]
  return self[field][used_locale] if self[field].key? used_locale
  return '' unless self[field].key? self.default_locale.to_s

  self[field][self.default_locale.to_s]
end

#is_field_localized(field, used_locale = nil) ⇒ Object



241
242
243
244
245
246
247
248
249
# File 'app/models/anoubis/core/application_record.rb', line 241

def is_field_localized(field, used_locale = nil)
  field = field.to_s.to_sym
  used_locale = self.current_locale.to_s unless used_locale

  return false unless self[field]
  return true if self[field].key? used_locale

  false
end

#new_uuidString

Generates new UUID data

Returns:

  • (String)

    string representation of UUID



170
171
172
# File 'app/models/anoubis/core/application_record.rb', line 170

def new_uuid
  SecureRandom.uuid
end

#redis_prefixObject

Returns defined application prefix for redis cache for current record. Default value ”



181
182
183
184
185
186
187
188
# File 'app/models/anoubis/core/application_record.rb', line 181

def redis_prefix
  begin
    value = Rails.configuration.redis_prefix
  rescue
    return ''
  end
  return value + ':'
end

#set_locale_field(field, value, used_locale = nil) ⇒ Object



232
233
234
235
236
237
238
239
# File 'app/models/anoubis/core/application_record.rb', line 232

def set_locale_field(field, value, used_locale = nil)
  field = field.to_s.to_sym
  used_locale = self.current_locale.to_s unless used_locale

  self[field] = {} unless self[field]
  self[field][self.default_locale.to_s] = value unless self[field].key? self.default_locale.to_s
  self[field][used_locale] = value
end