Class: Georgia::PagesController

Inherits:
ApplicationController show all
Includes:
Concerns::Helpers
Defined in:
app/controllers/georgia/pages_controller.rb

Instance Method Summary collapse

Methods included from Concerns::Helpers

#instance_name, #model

Methods inherited from ApplicationController

#current_ability, #current_locale

Instance Method Details

#copyObject

Creates a copy of a page and redirects to its revisions#edit



61
62
63
64
# File 'app/controllers/georgia/pages_controller.rb', line 61

def copy
  @copy = @page.copy
  redirect_to edit_page_revision_path(@copy, @copy.current_revision), notice: "Do not forget to change your url"
end

#createObject

Create page, load first current revision and js redirect to revisions#edit



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/georgia/pages_controller.rb', line 25

def create
  @page = model.new(slug: params[:title].try(:parameterize))
  if @page.save
    @page.revisions.create(template: Georgia.templates.first) do |rev|
      rev.contents << Georgia::Content.new(locale: I18n.default_locale, title: params[:title])
    end
    @page.update_attribute(:current_revision, @page.revisions.first)
    respond_to do |format|
      format.html { redirect_to edit_page_revision_path(@page, @page.current_revision), notice: "#{@page.title} was successfully created." }
      format.js { render layout: false }
    end
  else
    respond_to do |format|
      format.html { redirect_to :back, alert: "Oups. Something went wrong." }
      format.js { render layout: false }
    end
  end
end

#destroyObject

Destroys page and its revisions from page Also used to destroy multiple pages from table checkboxes



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/georgia/pages_controller.rb', line 68

def destroy
  back_url = url_for(controller: controller_name, action: :search)
  @pages = model.where(id: params[:id])
  if @pages.destroy_all
    respond_to do |format|
      format.html { redirect_to back_url, notice: "#{instance_name.humanize} successfully deleted." }
      format.js { head :ok }
    end
  else
    respond_to do |format|
      format.html { redirect_to back_url, alert: "Oups. Something went wrong." }
      format.js { head :internal_server_error }
    end
  end
end

#editObject

Edit current revision



16
17
18
# File 'app/controllers/georgia/pages_controller.rb', line 16

def edit
  redirect_to [:edit, @page, @page.current_revision]
end

#flush_cacheObject

Flush cache from multiple pages



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/controllers/georgia/pages_controller.rb', line 85

def flush_cache
  @pages = model.where(id: params[:id])
  @cache_keys = @pages.map(&:cache_key)
  unless @cache_keys.map{|k| expire_action(k)}.include?(false)
    respond_to do |format|
      format.html { redirect_to :back, notice: "Cache was successfully cleared." }
      format.js { head :ok }
    end
  else
    respond_to do |format|
      format.html { redirect_to :back, alert: "Oups. Either there wasn't any cache to start with or something went wrong." }
      format.js { head :internal_server_error }
    end
  end
end

#indexObject



146
147
148
# File 'app/controllers/georgia/pages_controller.rb', line 146

def index
  redirect_to search_pages_path
end

#publishObject

Publishes multiple pages from table checkboxes



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/controllers/georgia/pages_controller.rb', line 102

def publish
  @pages = model.where(id: params[:id])

  unless @pages.map(&:publish).include?(false)
    respond_to do |format|
      format.html { redirect_to :back, notice: "Successfully published." }
      format.js { head :ok }
    end
  else
    respond_to do |format|
      format.html { redirect_to :back, alert: "Oups. Something went wrong." }
      format.js { head :internal_server_error }
    end
  end
end

#searchObject



150
151
152
153
154
# File 'app/controllers/georgia/pages_controller.rb', line 150

def search
  session[:search_params] = params
  @search = Georgia::Indexer.adapter.search(model, params)
  @pages = Georgia::PageDecorator.decorate_collection(@search.results)
end

#settingsObject

Edit current page



21
22
# File 'app/controllers/georgia/pages_controller.rb', line 21

def settings
end

#showObject



11
12
13
# File 'app/controllers/georgia/pages_controller.rb', line 11

def show
  redirect_to [:edit, @page]
end

#sortObject

Sorts subpages/children from pages#settings FIXME: This should be taken care of in pages#update



137
138
139
140
141
142
143
144
# File 'app/controllers/georgia/pages_controller.rb', line 137

def sort
  if params[:page]
    params[:page].each_with_index do |id, index|
      model.update_all({position: index+1}, {id: id})
    end
  end
  render nothing: true
end

#unpublishObject

Unpublishes the page



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/controllers/georgia/pages_controller.rb', line 119

def unpublish
  @pages = model.where(id: params[:id])

  unless @pages.map(&:unpublish).include?(false)
    respond_to do |format|
      format.html { redirect_to :back, notice: "Successfully unpublished." }
      format.js { head :ok }
    end
  else
    respond_to do |format|
      format.html { redirect_to :back, alert: "Oups. Something went wrong." }
      format.js { head :internal_server_error }
    end
  end
end

#updateObject

Update page settings



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/georgia/pages_controller.rb', line 45

def update
  Georgia::Page.update_tree(params[:page_tree]) if params[:page_tree]
  if @page.update_attributes(params[:page])
    respond_to do |format|
      format.html { render :settings, notice: "#{@page.title} was successfully updated." }
      format.js { head :ok }
    end
  else
    respond_to do |format|
      format.html { redirect_to [:settings, @page], notice: "Oups. Something went wrong." }
      format.js { head :internal_server_error }
    end
  end
end