Class: Rich::Cms::Content::Group

Inherits:
Struct
  • Object
show all
Defined in:
lib/rich/cms/content/group.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#addObject

Returns the value of attribute add

Returns:

  • (Object)

    the current value of add



6
7
8
# File 'lib/rich/cms/content/group.rb', line 6

def add
  @add
end

#after_updateObject

Returns the value of attribute after_update

Returns:

  • (Object)

    the current value of after_update



6
7
8
# File 'lib/rich/cms/content/group.rb', line 6

def after_update
  @after_update
end

#before_editObject

Returns the value of attribute before_edit

Returns:

  • (Object)

    the current value of before_edit



6
7
8
# File 'lib/rich/cms/content/group.rb', line 6

def before_edit
  @before_edit
end

#class_nameObject

Returns the value of attribute class_name

Returns:

  • (Object)

    the current value of class_name



6
7
8
# File 'lib/rich/cms/content/group.rb', line 6

def class_name
  @class_name
end

#keyObject

Returns the value of attribute key

Returns:

  • (Object)

    the current value of key



6
7
8
# File 'lib/rich/cms/content/group.rb', line 6

def key
  @key
end

#selectorObject

Returns the value of attribute selector

Returns:

  • (Object)

    the current value of selector



6
7
8
# File 'lib/rich/cms/content/group.rb', line 6

def selector
  @selector
end

#tagObject

Returns the value of attribute tag

Returns:

  • (Object)

    the current value of tag



6
7
8
# File 'lib/rich/cms/content/group.rb', line 6

def tag
  @tag
end

#valueObject

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



6
7
8
# File 'lib/rich/cms/content/group.rb', line 6

def value
  @value
end

Class Method Details

.build(selector, specs) ⇒ Object



8
9
10
# File 'lib/rich/cms/content/group.rb', line 8

def self.build(selector, specs)
  self.new *{:selector => selector, :key => :key, :value => :value}.merge(specs).stringify_keys.values_at(*self.members)
end

Instance Method Details

#fetch(ref, as_content_item = true) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rich/cms/content/group.rb', line 12

def fetch(ref, as_content_item = true)
  reference = if ref.is_a?(Hash)
                ref
              elsif identifiers.size == 1
                {identifiers.first => ref}
              end
  reference.stringify_keys! if reference.is_a?(Hash)

  unless valid_reference?(reference)
    raise ArgumentError, "Invalid reference #{reference.inspect} (#{reference.values_at(*identifiers).inspect}) passed for #{identifiers.inspect}"
  end

  object = self.class_name.constantize.send :"find_or_initialize_by_#{identifiers.join "_and_"}", *reference.values_at(*identifiers)

  as_content_item ? Cms::Content::Item.new(self, object) : object
end

#identifiersObject



33
34
35
# File 'lib/rich/cms/content/group.rb', line 33

def identifiers
  @identifiers ||= [self.key].flatten.collect(&:to_s).sort
end

#keysObject



29
30
31
# File 'lib/rich/cms/content/group.rb', line 29

def keys
  (identifiers + [self.add || []].flatten.collect(&:to_s)).sort
end

#to_javascript_hashObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rich/cms/content/group.rb', line 37

def to_javascript_hash
  "#{self.selector.inspect}: {#{
    members.collect do |k|
      if v = self[k]
        case k
        when "key"
          "keys: [#{
          [v].flatten.collect do |key|
            "data-#{key}".inspect
          end.join ", "
          }]"
        when "value"
          "#{k}: #{"data-#{v}".inspect}"
        when "before_edit", "after_update"
          "#{k.camelize :lower}: #{v}"
        end
      end
    end.compact.join(", ")
  }}"
end