Class: DcSetup

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps
Defined in:
app/models/dc_setup.rb

Overview

DcSetup collection is used for settings, that are specific to the application, or part of application (gem). It consists of data dafinitions and form for editing data. Data is saved internaly in YAML format.

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

Usage:

my_app_settings = DcSetup.find_by(name: 'my_app')
my_app_settings = DcSetup.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.



75
76
77
78
79
80
81
82
83
# File 'app/models/dc_setup.rb', line 75

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)



107
108
109
# File 'app/models/dc_setup.rb', line 107

def my_data
  @my_data
end

#my_fieldsObject (readonly)

Returns the value of attribute my_fields.



44
45
46
# File 'app/models/dc_setup.rb', line 44

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



68
69
70
# File 'app/models/dc_setup.rb', line 68

def self.get(app_name)
  DcSetup.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.



89
90
91
92
93
# File 'app/models/dc_setup.rb', line 89

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

  keys.inject([]) { |r, k| r << my_data[k.to_s] }
end

#respond_to?(field_name) ⇒ Boolean

Will return true if setting is defined on the form

Returns:

  • (Boolean)


98
99
100
101
102
# File 'app/models/dc_setup.rb', line 98

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

  super.respond_to?(field_name)
end