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.



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

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)



108
109
110
# File 'app/models/ar_setup.rb', line 108

def my_data
  @my_data
end

#my_fieldsObject (readonly)

Returns the value of attribute my_fields.



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

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



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

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.



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

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.



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

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.



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

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

#respond_to?(field_name) ⇒ Boolean

Should always respond as true

Returns:

  • (Boolean)


101
102
103
# File 'app/models/ar_setup.rb', line 101

def respond_to?(field_name)
  true
end