Class: DeadSimpleCMS::Section
- Inherits:
-
Group
- Object
- Attribute::Collection
- Group
- DeadSimpleCMS::Section
- Extended by:
- ActiveModel::Callbacks
- Includes:
- ActiveModel::Observing
- Defined in:
- lib/dead_simple_cms/section.rb,
lib/dead_simple_cms/section/builder.rb
Overview
Public: Represents a section in the website a user wants to be able to modify the data in an immediate-simple way.
Defined Under Namespace
Classes: Builder
Constant Summary
Constants inherited from Group
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#root_group ⇒ Object
readonly
Returns the value of attribute root_group.
Attributes inherited from Group
Attributes inherited from Attribute::Collection
Class Method Summary collapse
-
.update_from_params(params) ⇒ Object
Public: Update the sections with the params and return the updated sections.
Instance Method Summary collapse
- #add_attribute(attribute) ⇒ Object
- #build(&block) ⇒ Object
- #cache_key ⇒ Object
- #fragments ⇒ Object
-
#initialize(identifier, options = {}, &block) ⇒ Section
constructor
A new instance of Section.
-
#refresh! ⇒ Object
Public: Clears out the storage so new values can be repopulated.
- #save! ⇒ Object
- #storage ⇒ Object
-
#update_attributes(attributes) ⇒ Object
Public: Update the section values.
Methods inherited from Group
#add_group, #extend, root, #root?
Methods included from Group::Presenter::RenderMixin
#display, #presenter_class, #render, #render_proc
Methods inherited from Attribute::Collection
Constructor Details
#initialize(identifier, options = {}, &block) ⇒ Section
Returns a new instance of Section.
21 22 23 24 25 26 27 28 29 |
# File 'lib/dead_simple_cms/section.rb', line 21 def initialize(identifier, ={}, &block) super(identifier, ) @path = [:path] @root_group = Group.root # The root group for the section. @fragments = Array.wrap([:fragments]) add_group(@root_group) build(&block) if block_given? end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
15 16 17 |
# File 'lib/dead_simple_cms/section.rb', line 15 def path @path end |
#root_group ⇒ Object (readonly)
Returns the value of attribute root_group.
15 16 17 |
# File 'lib/dead_simple_cms/section.rb', line 15 def root_group @root_group end |
Class Method Details
.update_from_params(params) ⇒ Object
Public: Update the sections with the params and return the updated sections.
32 33 34 35 36 37 38 |
# File 'lib/dead_simple_cms/section.rb', line 32 def self.update_from_params(params) params.map do |section_identifier, attributes| DeadSimpleCMS.sections[section_identifier].tap do |section| section.update_attributes(attributes) if section end end.compact end |
Instance Method Details
#add_attribute(attribute) ⇒ Object
40 41 42 43 |
# File 'lib/dead_simple_cms/section.rb', line 40 def add_attribute(attribute) super attribute.section = self end |
#build(&block) ⇒ Object
76 77 78 |
# File 'lib/dead_simple_cms/section.rb', line 76 def build(&block) Builder.new(self, &block) end |
#cache_key ⇒ Object
80 81 82 |
# File 'lib/dead_simple_cms/section.rb', line 80 def cache_key "cms/#{identifier}/#{to_param}" end |
#fragments ⇒ Object
45 46 47 |
# File 'lib/dead_simple_cms/section.rb', line 45 def fragments @fragments.map { |f| f.is_a?(Proc) ? f.call(self) : f }.flatten.uniq end |
#refresh! ⇒ Object
Public: Clears out the storage so new values can be repopulated. Used primarily to clear the values between refreshes.
54 55 56 |
# File 'lib/dead_simple_cms/section.rb', line 54 def refresh! @storage = nil end |
#save! ⇒ Object
72 73 74 |
# File 'lib/dead_simple_cms/section.rb', line 72 def save! _run_save_callbacks { storage.write } end |
#storage ⇒ Object
49 50 51 |
# File 'lib/dead_simple_cms/section.rb', line 49 def storage @storage ||= storage_class.new(self) end |
#update_attributes(attributes) ⇒ Object
Public: Update the section values.
Examples
update_attributes("left_attributes" => {"header" => "Hi There", "paragraph" => "how's it going"}})
update_attributes("left_header" => "Hi There", "left_paragraph" => "how's it going"})
Both of these work assuming the user defined a group called “left” and has attributes in it called “header” and “paragraph”. The “left_attributes=” method plays nicely with nested fields_for which the form builder dependes on.
67 68 69 70 |
# File 'lib/dead_simple_cms/section.rb', line 67 def update_attributes(attributes) super save! end |