Class: Effective::Snippets::Snippet

Inherits:
Object
  • Object
show all
Defined in:
app/models/effective/snippets/snippet.rb

Direct Known Subclasses

CurrentDateTime, CurrentUserInfo

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(atts = {}) ⇒ Snippet

If you define “attribute :something, Array” in your derived class You can call effective_region post, :content :snippet_locals => => [1,2,3] And it will be assigned when the effective_region is rendered



60
61
62
63
# File 'app/models/effective/snippets/snippet.rb', line 60

def initialize(atts = {})
  snippet_attributes.each { |name| self.class.send(:attr_accessor, name) }
  (atts || {}).each { |k, v| self.send("#{k}=", v) if respond_to?("#{k}=") }
end

Class Method Details

.all(type = nil) ⇒ Object

This is going to return all snippet objects that are saved in any Effective::Regions



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/effective/snippets/snippet.rb', line 17

def self.all(type = nil)
  if type.present?
    name = case type
    when Snippet
      type.class_name.to_s
    when Class
      type.name.demodulize.underscore
    when String
      type.demodulize.underscore
    else
      raise 'Expected a class name, an instance of a snippet, or a string'
    end.to_sym

    Effective::Region.with_snippets
      .where("#{EffectiveRegions.regions_table_name}.snippets ILIKE ?", "%class_name: #{name}%")
      .flat_map { |region| region.snippet_objects }
      .select { |snippet| snippet.class_name == name }
  else
    Effective::Region.with_snippets.flat_map { |region| region.snippet_objects }
  end
end

.definitions(controller = nil) ⇒ Object

This is used by the effective_regions_helper effective_regions_include_tags And ends up in the javascript CKEDITOR.config top level namespace



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/effective/snippets/snippet.rb', line 41

def self.definitions(controller = nil)
  {}.tap do |snippets|
    EffectiveRegions.snippets.each do |snippet|
      snippets[snippet.class_name] = {
        :dialog_url => snippet.snippet_dialog_url,
        :label => snippet.snippet_label,
        :description => snippet.snippet_description,
        :inline => snippet.snippet_inline,
        :editables => snippet.snippet_editables,
        :tag => snippet.snippet_tag.to_s
      }
    end
  end
end

Instance Method Details

#class_nameObject



81
82
83
# File 'app/models/effective/snippets/snippet.rb', line 81

def class_name
  @class_name ||= self.class.name.demodulize.underscore.to_sym
end

#dataObject



73
74
75
# File 'app/models/effective/snippets/snippet.rb', line 73

def data
  (self.snippet_attributes - [:region, :id]).inject({}) { |h, name| h[name] = public_send(name); h}
end

#idObject



65
66
67
# File 'app/models/effective/snippets/snippet.rb', line 65

def id
  @id || "snippet_#{object_id}"
end

#regionObject



69
70
71
# File 'app/models/effective/snippets/snippet.rb', line 69

def region
  @region || Effective::Region.new
end

#snippet_attributesObject

Each Snippet has to be a block (or inline) element with nested children. It has to start with a root object That root object has to do snippet_data(snippet) attr_accessor :id # This will be snippet_12345 attr_accessor :region # The region Object



12
13
14
# File 'app/models/effective/snippets/snippet.rb', line 12

def snippet_attributes
  [:id, :region]
end

#snippet_descriptionObject



90
91
92
# File 'app/models/effective/snippets/snippet.rb', line 90

def snippet_description
  "Insert #{snippet_label}"
end

#snippet_dialog_urlObject



94
95
96
# File 'app/models/effective/snippets/snippet.rb', line 94

def snippet_dialog_url
  "/assets/effective/snippets/#{class_name}.js"
end

#snippet_editablesObject



109
110
111
# File 'app/models/effective/snippets/snippet.rb', line 109

def snippet_editables
  false
end

#snippet_inlineObject



105
106
107
# File 'app/models/effective/snippets/snippet.rb', line 105

def snippet_inline
  [:span].include?(snippet_tag)
end

#snippet_labelObject

The following methods are used for the CKEditor widget creation.



86
87
88
# File 'app/models/effective/snippets/snippet.rb', line 86

def snippet_label
  class_name.to_s.humanize
end

#snippet_tagObject

This is the tag that the ckeditor snippet will be created as It supports divs and spans, but that’s it No ULs, or LIs



101
102
103
# File 'app/models/effective/snippets/snippet.rb', line 101

def snippet_tag
  :div
end

#to_partial_pathObject



77
78
79
# File 'app/models/effective/snippets/snippet.rb', line 77

def to_partial_path
  "effective/snippets/#{class_name}"
end