Module: Cobranding::PersistentLayout
- Defined in:
- lib/cobranding/persistent_layout.rb
Overview
This module can be mixed in to persistent objects so that layouts can be persisted to a data store. This is very useful when there are only a few layouts and they don’t need to be updated in real time. In this case, you can run a background task to call fetch_layout on all your persistent layouts to update them asynchronously.
By default, it will be assumed that the URL for the layout service will be stored in a field called url and the compiled Layout ruby code will be stored in src. You can override these values with layout_src_attribute and layout_url_attriubte. In addition, if the URL takes options, you can specify the field that stores the Hash with layout_url_options_attribute.
If the layout code needs to be munged, set the :layout_preprocessor class attribute to either a symbol that matches a method name or to a Proc.
Class Method Summary collapse
Instance Method Summary collapse
-
#fetch_layout ⇒ Object
Fetch a loyout from the service and store the ruby src code in the src attribute.
-
#layout ⇒ Object
Get the layout defined by the src attribute.
- #layout_html=(html) ⇒ Object
Class Method Details
.included(base) ⇒ Object
14 15 16 |
# File 'lib/cobranding/persistent_layout.rb', line 14 def self.included (base) base.class_attribute :layout_src_attribute, :layout_url_attribute, :layout_url_options_attribute, :layout_preprocessor end |
Instance Method Details
#fetch_layout ⇒ Object
Fetch a loyout from the service and store the ruby src code in the src attribute.
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/cobranding/persistent_layout.rb', line 19 def fetch_layout layout_url = send(layout_url_attribute || :url) unless layout_url.blank? = send() unless .blank? ||= {} preprocessor = self.class.layout_preprocessor if preprocessor && !preprocessor.is_a?(Proc) preprocessor = method(preprocessor) end @layout = preprocessor ? Layout.get(layout_url, , &preprocessor) : Layout.get(layout_url, ) send("#{self.class.layout_src_attribute || :src}=", @layout.src) end end |
#layout ⇒ Object
Get the layout defined by the src attribute.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/cobranding/persistent_layout.rb', line 34 def layout unless @layout layout_src = send(layout_src_attribute || :src) unless layout_src.blank? @layout = Layout.new @layout.src = layout_src end end @layout end |
#layout_html=(html) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/cobranding/persistent_layout.rb', line 45 def layout_html= (html) preprocessor = self.class.layout_preprocessor preprocessor = method(preprocessor) if preprocessor && !preprocessor.is_a?(Proc) html = preprocessor.call(html) if preprocessor @layout = Layout.new(html) send("#{self.class.layout_src_attribute || :src}=", @layout.src) end |