Class: Rich::Cms::Content::Item
- Inherits:
-
Object
- Object
- Rich::Cms::Content::Item
- Defined in:
- lib/rich/cms/content/item.rb
Instance Method Summary collapse
-
#initialize(*args) ⇒ Item
constructor
A new instance of Item.
- #save ⇒ Object
- #to_tag(options = {}) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Item
Returns a new instance of Item.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/rich/cms/content/item.rb', line 8 def initialize(*args) if args.size == 1 && args.first.is_a?(Hash) hash = args.first elsif args.size == 2 array = args else raise ArgumentError, "Invalid arguments #{args.inspect} passed for #{self.class.name}" end if hash && (selector = hash.stringify_keys["__selector__"]) @group = Engine.editable_content[selector] @object = @group.fetch hash, false attributes, params = hash.partition{|k, v| @object.attribute_names.include? k.to_s} @object.attributes = Hash[*attributes.flatten] @params = HashWithIndifferentAccess[*params.flatten] elsif array && array.first.is_a?(Group) && array.last.is_a?(::ActiveRecord::Base) @group, @object = *array end end |
Instance Method Details
#save ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rich/cms/content/item.rb', line 31 def save @object.save if @object.respond_to? :to_rich_cms_response hash = @object.to_rich_cms_response @params else keys = @group.keys << @group.value.to_s hash = @object.attributes.reject{|k, v| !keys.include?(k.to_s)} end selector = @group.selector identifier = @group.identifiers.inject({}){|h, k| h[k] = @object.send(k); h} hash.merge({:__selector__ => selector, :__identifier__ => identifier}) end |
#to_tag(options = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/rich/cms/content/item.rb', line 47 def to_tag( = {}) attrs = [] default = @group.identifiers.size == 1 ? @object.send(@group.identifiers.first) : @object.attributes.values_at(*@group.identifiers).inspect value = @object.send(@group.value) if Engine. default = "< #{default} >" keys = @group.keys << @group.value.to_s data = @object.attributes.reject{|k, v| !keys.include?(k.to_s)} data[:editable_input_type] = [:as] if %w(string text html).include? [:as].to_s.downcase if class_name = @group.selector.match(/^\.\w+$/) ([:html] ||= {}).store :class, [class_name.to_s.gsub(/^\./, ""), [:html].try(:fetch, :class, nil)].compact.join(" ") end attrs << [:html].collect{|k, v| "#{k}=\"#{::ERB::Util.html_escape v}\""}.join(" ") if [:html] attrs << data .collect{|k, v| "data-#{k}=\"#{::ERB::Util.html_escape v}\""}.join(" ") end tag = [:tag] || @group.tag || (%w(text html).include?([:as].to_s.downcase) ? :div : :span) "<#{tag} #{attrs.join(" ")}>#{value.blank? ? default : value}</#{tag}>".html_safe end |