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



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

def add
  @add
end

#after_updateObject

Returns the value of attribute after_update

Returns:

  • (Object)

    the current value of after_update



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

def after_update
  @after_update
end

#before_editObject

Returns the value of attribute before_edit

Returns:

  • (Object)

    the current value of before_edit



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

def before_edit
  @before_edit
end

#class_nameObject

Returns the value of attribute class_name

Returns:

  • (Object)

    the current value of class_name



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

def class_name
  @class_name
end

#keyObject

Returns the value of attribute key

Returns:

  • (Object)

    the current value of key



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

def key
  @key
end

#selectorObject

Returns the value of attribute selector

Returns:

  • (Object)

    the current value of selector



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

def selector
  @selector
end

#tagObject

Returns the value of attribute tag

Returns:

  • (Object)

    the current value of tag



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

def tag
  @tag
end

#valueObject

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



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

def value
  @value
end

Class Method Details

.build(selector, specs) ⇒ Object



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

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



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

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



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

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

#keysObject



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

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

#to_javascript_hashObject



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

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