Class: ArSetup

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/ar_setup.rb

Overview

ArSetup table is for settings, that are specific to the application, or part of application (gem). It consists of data definitions and form for editing the data. Data is saved internally in YAML format.

When editing, admin can see and edit form definition (adding new fields to application setup), while user sees only data entry form.

Usage:

my_app_settings = ArSetup.find_by(name: 'my_app')
my_app_settings = ArSetup.get('my_app')
company = my_app_settings.company_name
company, ceo = my_app_settings[:company_name, 'ceo_name']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

Will return value for single setting if called as method.



79
80
81
82
83
84
85
86
87
# File 'app/models/ar_setup.rb', line 79

def method_missing(m, *args, &block)
  m = m.to_s
  if m.match('=')
    m.chomp!('=')
    my_data[m] = args.first
  else
    my_data[m]
  end
end

Instance Attribute Details

#my_dataObject (readonly)



111
112
113
# File 'app/models/ar_setup.rb', line 111

def my_data
  @my_data
end

#my_fieldsObject (readonly)

Returns the value of attribute my_fields.



42
43
44
# File 'app/models/ar_setup.rb', line 42

def my_fields
  @my_fields
end

Class Method Details

.get(app_name) ⇒ Object?

Will return settings record for specified application.

Parameters:

  • app_name (String)

    The name of the application

Returns:

  • (Object, nil)

    The settings record if found, nil otherwise



72
73
74
# File 'app/models/ar_setup.rb', line 72

def self.get(app_name)
  ArSetup.find_by(name: app_name.to_s)
end

Instance Method Details

#[](*keys) ⇒ Object

Will return value for single setting. Called as parameter in square brackets. If more then one parameter is passed it will return them as array.



93
94
95
96
97
# File 'app/models/ar_setup.rb', line 93

def [](*keys)
  return my_data[keys.first.to_s] if keys.size == 1

  keys.map { |k| my_data[k.to_s] }
end

#editorsObject

Will return editors var as array to the application. Editors are internaly saved as string separated by comma.



54
55
56
# File 'app/models/ar_setup.rb', line 54

def editors
  edit_ids.to_s.split(',').map(&:to_i)
end

#editors=(value) ⇒ Object

Will return editors var as array to the application. Editors are internaly saved as string separated by comma.



62
63
64
# File 'app/models/ar_setup.rb', line 62

def editors=(value)
  self.edit_ids = value.join(',')
end

#respond_to?(field_name) ⇒ Boolean

Will return true if setting is defined on the form

Returns:

  • (Boolean)


102
103
104
105
106
# File 'app/models/ar_setup.rb', line 102

def respond_to?(field_name)
  return true #if my_fields[field_name.to_s]

  super.respond_to?(field_name)
end