Module: EffectiveRegionsHelper

Defined in:
app/helpers/effective_regions_helper.rb

Instance Method Summary collapse

Instance Method Details

#ckeditor_region(args, options = {}, &block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/helpers/effective_regions_helper.rb', line 41

def ckeditor_region(args, options = {}, &block)
  obj = args.first
  title = args.last.to_s.parameterize
  editable_tag = options.delete(:editable_tag) || :div
  snippet_locals = options.delete(:snippet_locals) # These get passed into the Snippet.new() model

  # Set up the editable div options we need to send to ckeditor
  if effectively_editing?
    opts = {
      :contenteditable => true,
      'data-effective-ckeditor' => (options.delete(:type) || :full).to_s,
      'data-allowed-snippets' => [options.delete(:snippets)].flatten.compact.to_json,
      :style => ['-webkit-user-modify: read-write;', options.delete(:style), ('display: inline;' if options.delete(:inline))].compact.join(' '),
      :class => ['effective-region', options.delete(:class)].compact.join(' ')
    }.merge(options)
  end

  if obj.kind_of?(ActiveRecord::Base)
    raise StandardError.new('Object passed to effective_region helper must declare act_as_regionable') unless obj.respond_to?(:acts_as_regionable)

    region = obj.regions.find { |region| region.title == title }

    if effectively_editing?
      can_edit = EffectiveResources.authorized?(controller, :update, obj)
      opts[:id] = [model_name_from_record_or_class(obj).param_key(), obj.id, title].join('_')
    end
  else # This is a global region
    regions = (@effective_regions_global ||= Effective::Region.global.to_a)
    region = regions.find { |region| region.title == title } || Effective::Region.new(:title => title)

    if effectively_editing?
      can_edit = EffectiveResources.authorized?(controller, :update, region)
      opts[:id] = title.to_s.parameterize
    end
  end

  if effectively_editing? && (can_edit && options[:editable] != false) # If we need the editable div
    (editable_tag, opts) do
      region.try(:content).nil? ? (capture(&block) if block_given?) : render_region(region, true, snippet_locals)
    end
  else
    region.try(:content).nil? ? (capture(&block) if block_given?) : render_region(region, false, snippet_locals)
  end
end

#effective_region(*args) ⇒ Object



2
3
4
5
# File 'app/helpers/effective_regions_helper.rb', line 2

def effective_region(*args)
  options = args.extract_options!
  block_given? ? ckeditor_region(args, options) { yield } : ckeditor_region(args, options)
end

#effective_regions_include_tagsObject

Loads the Ckeditor Javascript & Stylesheets only when in edit mode



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/helpers/effective_regions_helper.rb', line 23

def effective_regions_include_tags
  if effectively_editing?
    payload = {
      :snippets => Effective::Snippets::Snippet.definitions(controller),
      :templates => Effective::Templates::Template.definitions(controller)
    }

    if defined?(EffectivePages) && defined?(EffectiveRoles)
      if(menu = Effective::Menu.new()).respond_to?(:is_role_restricted?)
        payload[:roles] = EffectiveRoles.roles_collection(menu, current_user)
        payload[:pages] = ([['', '']] + Effective::Page.order(:title).map { |page| [page.title, page.id] })
      end
    end

    render(:partial => 'effective_regions/include_tags_javascript', :locals => {:payload => payload})
  end
end

#render_region(region, can_edit = true, snippet_locals = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'app/helpers/effective_regions_helper.rb', line 86

def render_region(region, can_edit = true, snippet_locals = {})
  return '' unless region

  region.content.tap do |html|
    html.scan(/\[(snippet_\d+)\]/).flatten.uniq.each do |id| # find snippet_1 and replace with snippet content
      snippet = region.snippet_objects(snippet_locals).find { |snippet| snippet.id == id }
      html.gsub!("[#{id}]", render_snippet(snippet, can_edit)) if snippet
    end
  end.html_safe
end

#render_snippet(snippet, can_edit = true) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/helpers/effective_regions_helper.rb', line 97

def render_snippet(snippet, can_edit = true)
  return '' unless snippet

  if Rails.env.production?
    content = render(:partial => snippet.to_partial_path, :object => snippet, :locals => {:snippet => snippet}) rescue ''
  else
    content = render(:partial => snippet.to_partial_path, :object => snippet, :locals => {:snippet => snippet})
  end

  if effectively_editing? && can_edit
    (snippet.snippet_tag, content, :data => {'effective-snippet' => snippet.class_name, 'snippet-data' => snippet.data().to_json})
  else
    content
  end.html_safe
end

#simple_effective_region(*args) ⇒ Object



7
8
9
10
# File 'app/helpers/effective_regions_helper.rb', line 7

def simple_effective_region(*args)
  (options = args.extract_options!).merge!(:type => :simple)
  block_given? ? ckeditor_region(args, options) { yield } : ckeditor_region(args, options)
end

#snippet_effective_region(*args) ⇒ Object



12
13
14
15
# File 'app/helpers/effective_regions_helper.rb', line 12

def snippet_effective_region(*args)
  (options = args.extract_options!).merge!(:type => :snippets)
  block_given? ? ckeditor_region(args, options) { yield } : ckeditor_region(args, options)
end

#wrapped_snippet_effective_region(*args) ⇒ Object



17
18
19
20
# File 'app/helpers/effective_regions_helper.rb', line 17

def wrapped_snippet_effective_region(*args)
  (options = args.extract_options!).merge!(:type => :wrapped_snippets)
  block_given? ? ckeditor_region(args, options) { yield } : ckeditor_region(args, options)
end