Class: Anoubis::ApplicationRecord

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

Overview

Main application model record class inherited from ActiveRecord::Base

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#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



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

def need_refresh
  @need_refresh
end

#redisClass

Returns Redis database class

Returns:

  • (Class)

    Redis class reference



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

def redis
  @redis
end

Class Method Details

.redisClass

Returns Redis database class

Returns:

  • (Class)

    Redis class reference



38
39
40
# File 'app/models/anoubis/application_record.rb', line 38

def self.redis
  Redis.new
end

.redis_prefixString

Returns Redis prefix for storing cache data. Prefix can be set in Rails.configuration.anoubis_redis_prefix configuration parameter.

Returns:

  • (String)

    Redis prefix



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

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

Instance Method Details

#after_initialize_anubis_application_recordObject

Fires after initialize default variables



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

def after_initialize_anubis_application_record

end

#can_delete(args = {}) ⇒ Boolean

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

Parameters:

  • args (Hash) (defaults to: {})

    transferred parameters

Options Hash (args):

  • :controller (String)

    Called controller identifier

  • :tab (String)

    Called controller tab element

Returns:

  • (Boolean)

    true if data may be deleted



183
184
185
# File 'app/models/anoubis/application_record.rb', line 183

def can_delete(args = {})
  true
end

#can_destroy?Boolean

Checks if this record may be destroyed.

Returns:

  • (Boolean)


138
139
140
141
142
143
144
145
# File 'app/models/anoubis/application_record.rb', line 138

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

#can_edit(args = {}) ⇒ Boolean

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

Parameters:

  • args (Hash) (defaults to: {})

    transferred parameters

Options Hash (args):

  • :controller (String)

    Called controller identifier

  • :tab (String)

    Called controller tab element

Returns:

  • (Boolean)

    true if data may be edited



172
173
174
# File 'app/models/anoubis/application_record.rb', line 172

def can_edit(args = {})
  true
end

#can_new(args = {}) ⇒ Boolean

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

Parameters:

  • args (Hash) (defaults to: {})

    transferred parameters

Options Hash (args):

  • :controller (String)

    Called controller identifier

  • :tab (String)

    Called controller tab element

Returns:

  • (Boolean)

    true if new data may be created.



161
162
163
# File 'app/models/anoubis/application_record.rb', line 161

def can_new(args = {})
  true
end

#current_localeString

Return defined locale according by I18n

Returns:

  • (String)

    current locale



69
70
71
# File 'app/models/anoubis/application_record.rb', line 69

def current_locale
  I18n.locale.to_s
end

#default_localeString

Default locale that setup in Rails.configuration.i18n.default_locale configuration parameter

Returns:

  • (String)

    default locale



87
88
89
# File 'app/models/anoubis/application_record.rb', line 87

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

#get_localeString

Returns #current_locale. If current locale isn’t set then returns #default_locale.

Returns:

  • (String)

    current locale



76
77
78
79
80
81
82
# File 'app/models/anoubis/application_record.rb', line 76

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

  default_locale
end

#get_locale_field(field, used_locale = nil) ⇒ String

Returns localized field by identifier

Parameters:

  • field (String)

    Field identifier

  • used_locale (String | nil) (defaults to: nil)

    Locale identifier (by default used #current_locale)

Returns:

  • (String)

    localized field



96
97
98
99
100
101
102
103
104
105
# File 'app/models/anoubis/application_record.rb', line 96

def get_locale_field(field, used_locale = nil)
  field = field.to_s.to_sym
  used_locale = 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? default_locale.to_s

  self[field][default_locale.to_s]
end

#initialize_anubis_application_recordObject

Fires after ApplicationRecord initialized for define default values



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

def initialize_anubis_application_record
  self.need_refresh = false

  after_initialize_anubis_application_record
end

#is_field_localized(field, used_locale = nil) ⇒ Boolean

Returns true if field has localized data

Parameters:

  • field (String)

    Field identifier

  • used_locale (String | nil) (defaults to: nil)

    Locale identifier (by default used #current_locale)

Returns:

  • (Boolean)

    true if field has localized data



126
127
128
129
130
131
132
133
134
# File 'app/models/anoubis/application_record.rb', line 126

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

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

  false
end

#redis_prefixString

Returns Redis prefix for storing cache data. Prefix can be set in Rails.configuration.anoubis_redis_prefix configuration parameter.

Returns:

  • (String)

    Redis prefix



45
46
47
48
49
50
51
52
# File 'app/models/anoubis/application_record.rb', line 45

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

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

Sets localized data

Parameters:

  • field (String)

    field identifier

  • value (String)

    localized string

  • used_locale (String | nil) (defaults to: nil)

    Locale identifier (by default used #current_locale)



112
113
114
115
116
117
118
119
# File 'app/models/anoubis/application_record.rb', line 112

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

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

#sys_titleString

Returns system title of table element

Returns:

  • (String)

    System title



150
151
152
# File 'app/models/anoubis/application_record.rb', line 150

def sys_title
  id.to_s
end