Class: Effective::RegionsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/effective/regions_controller.rb

Instance Method Summary collapse

Instance Method Details

#editObject



11
12
13
14
15
16
17
18
# File 'app/controllers/effective/regions_controller.rb', line 11

def edit
  EffectiveRegions.authorized?(self, :edit, Effective::Region.new())

  cookies['effective_regions_editting'] = {:value => params[:exit].presence || request.referrer, :path => '/'}

  # TODO: turn this into a cookie or something better.
  redirect_to request.url.gsub('/edit', '') + '?edit=true'
end

#snippetObject

This is a GET. CKEDITOR passes us data, we need to render the non-editable content



89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/effective/regions_controller.rb', line 89

def snippet # This is a GET.  CKEDITOR passes us data, we need to render the non-editable content
  klass = "Effective::Snippets::#{region_params[:name].try(:classify)}".safe_constantize

  if klass.present?
    @snippet = klass.new(region_params[:data])
    render :partial => @snippet.to_partial_path, :object => @snippet, :locals => {:snippet => @snippet, :snippet_preview => true}
  else
    render :text => "Missing class Effective::Snippets::#{region_params[:name].try(:classify)}"
  end
end

#snippetsObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/effective/regions_controller.rb', line 70

def snippets
  EffectiveRegions.authorized?(self, :edit, Effective::Region.new())

  retval = {}
  EffectiveRegions.snippets.each do |snippet|
    retval[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
      #:template => ActionView::Base.new(ActionController::Base.view_paths, {}, ActionController::Base.new).render(:partial => snippet.to_partial_path, :object => snippet, :locals => {:snippet => snippet})
    }
  end

  render :json => retval
end

#templatesObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/controllers/effective/regions_controller.rb', line 100

def templates
  EffectiveRegions.authorized?(self, :edit, Effective::Region.new())

  retval = EffectiveRegions.templates.map do |template|
    {
      :title => template.title,
      :description => template.description,
      :image => template.image || "#{template.class_name}.png",
      :html => render_to_string(:partial => template.to_partial_path, :object => template, :locals => {:template => template})
    }
  end

  render :json => retval
end

#updateObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
# File 'app/controllers/effective/regions_controller.rb', line 20

def update
  javascript_should_refresh_page = ''

  Effective::Region.transaction do
    (request.fullpath.slice!(0..4) rescue nil) if request.fullpath.to_s.starts_with?('/edit') # This is so the before_save_method can reference the real current page

    region_params.each do |key, vals| # article_section_2_title => {:content => '<p></p>'}
      to_save = nil  # Which object, the regionable, or the region (if global) to save

      regionable, title = find_regionable(key)

      if regionable
        EffectiveRegions.authorized?(self, :update, regionable) # can I update the regionable object?

        region = regionable.regions.find { |region| region.title == title }
        region ||= regionable.regions.build(:title => title)

        to_save = regionable
      else
        region = Effective::Region.global.where(:title => title).first_or_initialize
        EffectiveRegions.authorized?(self, :update, region) # can I update the global region?

        to_save = region
      end

      region.content = cleanup(vals[:content])

      region.snippets = HashWithIndifferentAccess.new()
      (vals[:snippets] || []).each { |snippet, vals| region.snippets[snippet] = vals }

      # Last chance for a developer to make some changes here
      if (run_before_save_method(region, regionable) rescue nil) == :refresh
        javascript_should_refresh_page = 'refresh'
      end

      to_save.save!
    end

    # Hand off the appropriate params to EffectivePages gem
    if defined?(EffectivePages)
      Effective::Menu.update_from_effective_regions!(params[:effective_menus])
    end

    render :text => javascript_should_refresh_page, :status => 200
    return
  end

  render :text => 'error', :status => :unprocessable_entity
end