Module: Skyline::Settings::ClassMethods

Defined in:
lib/skyline/settings.rb

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Object

We have to make sure always the same object will be retrieved Do a forcefull reload if you want to get a new instance from the database



59
60
61
62
63
# File 'lib/skyline/settings.rb', line 59

def [](name)
  f = self.find(:first, :conditions => {:page => name.to_s})
  f ||= create(:page => name.to_s, :data => HashWithIndifferentAccess.new)
  f
end

#get(setting_identifier, field) ⇒ Object

A safe way to get a value of a setting and report a warning if it can’t be found

instead of calling Setting.field directly use setting(:setting_identifier, :field)

Parameters:

  • setting_identifier (Symbol)

    the symbol of the settings page

  • field (Symbol)

    the name of the setting

Returns:

  • (Object)

    the value of the setting or nil if not found



72
73
74
75
76
77
78
79
80
# File 'lib/skyline/settings.rb', line 72

def get(setting_identifier, field)
  if s = self[setting_identifier]
    if s.respond_to?(field)
      return s.send(field)
    end
  end
  Rails.logger.warn "Couldn't find Setting[:#{setting_identifier}].#{field}"
  nil
end

#get_page(setting_identifier, field) ⇒ Page, NilClass

a safe way to get a page from the settings

Parameters:

  • setting_identifier (Symbol)

    the symbol of the settings page

  • field (Symbol)

    the name of the setting that references a page_id

Returns:

  • (Page, NilClass)

    The page if found, nil otherwise



88
89
90
91
92
93
# File 'lib/skyline/settings.rb', line 88

def get_page(setting_identifier, field)
  if page_id = self.get(setting_identifier, field)
    return Skyline::Page.find_by_id(page_id) if page_id.present?
  end        
  nil
end

#ordered_pagesObject

The pages in order of page_order, returns an array of page objects.



47
48
49
# File 'lib/skyline/settings.rb', line 47

def ordered_pages
  page_order.map{|name| pages[name]}
end

#page(name, options = {}) {|page| ... } ⇒ Object

Define a page in the object, see example below

class DummyImplementation::Settings < DummyImplementation::ImplementationSettings
  page :general do |p|
    p.description "Here you can edit all your general settings"
    p.field :address, :type => :text
    p.field :newsletter, :type => :text    
    p.field :mission_statement, :type => :text
  end
  page :test do |p|
    p.description "A test of all possible options"
    p.field_group :name do |g|
      g.field :firstname, :type => :string
      g.field :lastname, :type => :string
    end
    p.field :birthdate, :type => :date
    p.field :today, :type => :datetime
    p.field :nr_of_kids, :type => :integer
    p.field :price_of_favourite_food, :type => :float
    p.field :loves_meat, :type => :boolean
  end    
end

Yields:



32
33
34
35
36
37
38
39
# File 'lib/skyline/settings.rb', line 32

def page(name,options={},&block)
  page = Skyline::Content::MetaData::FieldPage.new(self,name,options)
  yield(page)
  create_proxy_method(name)
  pages.update(name => page)
  page_order << page.name
  page
end

#page_orderObject Also known as: page_names

Return the page order as an array of page names



52
53
54
# File 'lib/skyline/settings.rb', line 52

def page_order
  @page_order ||= []
end

#pagesObject

The pages hash, access pages like this: Settings.pages to get the settingspage definition



42
43
44
# File 'lib/skyline/settings.rb', line 42

def pages
  @pages ||= {}
end

#referable_serialized_content(*fields) ⇒ Object

Can be used to reference a Page/MediaFile etc directly. Sets it to ‘FIELD_id`.

Parameters:

  • field (String, Symbol)

    The field name to use (multiple possible)



99
100
101
102
103
104
105
106
107
# File 'lib/skyline/settings.rb', line 99

def referable_serialized_content(*fields)
  fields.each do |f|
    self.class_eval <<-END
      def #{f}_attributes=(attr)
        self.#{f}_id = attr[:referable_id]
      end
    END
  end
end