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
19
20
21
# File 'app/controllers/effective/regions_controller.rb', line 11

def edit
  EffectiveResources.authorize!(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.
  uri = URI.parse(Rack::Utils.unescape(request.url.sub('/edit', '')))
  uri.query = [uri.query, "edit=true"].compact.join('&')

  redirect_to uri.to_s
end

#snippetObject

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



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

def snippet # This is a GET.  CKEDITOR passes us data, we need to render the non-editable content
  EffectiveResources.authorize!(self, :edit, Effective::Region.new)

  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

#updateObject



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
69
70
71
72
# File 'app/controllers/effective/regions_controller.rb', line 23

def update
  refresh_page = false
  response = {}

  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
        EffectiveResources.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
        EffectiveResources.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] = HashWithIndifferentAccess.new(vals.to_h) }

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

      to_save.save!
    end

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

    response[:refresh] = true if refresh_page

    render(json: response.to_json(), status: 200)
    return
  end

  render(text: 'error', status: :unprocessable_entity)
end