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
- #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]) @block = block add_group(@root_group) @is_loaded = false 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.
37 38 39 40 41 42 43 |
# File 'lib/dead_simple_cms/section.rb', line 37 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
45 46 47 48 |
# File 'lib/dead_simple_cms/section.rb', line 45 def add_attribute(attribute) super attribute.section = self end |
#build(&block) ⇒ Object
81 82 83 |
# File 'lib/dead_simple_cms/section.rb', line 81 def build(&block) Builder.new(self, &block) end |
#build_block! ⇒ Object
31 32 33 34 |
# File 'lib/dead_simple_cms/section.rb', line 31 def build_block! build(&@block) if @block && !@is_loaded @is_loaded = true end |
#cache_key ⇒ Object
85 86 87 |
# File 'lib/dead_simple_cms/section.rb', line 85 def cache_key "cms/#{identifier}/#{to_param}" end |
#fragments ⇒ Object
50 51 52 |
# File 'lib/dead_simple_cms/section.rb', line 50 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.
59 60 61 |
# File 'lib/dead_simple_cms/section.rb', line 59 def refresh! @storage = nil end |
#save! ⇒ Object
77 78 79 |
# File 'lib/dead_simple_cms/section.rb', line 77 def save! _run_save_callbacks { storage.write } end |
#storage ⇒ Object
54 55 56 |
# File 'lib/dead_simple_cms/section.rb', line 54 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.
72 73 74 75 |
# File 'lib/dead_simple_cms/section.rb', line 72 def update_attributes(attributes) super save! end |