Class: Admin::AdminController

Inherits:
ApplicationController
  • Object
show all
Includes:
AdminHelper
Defined in:
app/controllers/admin/admin_controller.rb

Overview

Public: A base controller for admin pages.

Instance Method Summary collapse

Instance Method Details

#createObject

POST /:things



92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/admin/admin_controller.rb', line 92

def create
  @thing = thing.new(thing_params)
  @route_name ||= @controller_name ||= controller_name

  if @thing.save
    flash[:notice] = "#{thing} successfully created."
    redirect_to things_path(@route_name), notice: "#{thing.to_s.titleize} successfully created."
  else
    render 'admin/new'
  end
end

#destroyObject

DELETE /:things/:id



116
117
118
119
120
121
# File 'app/controllers/admin/admin_controller.rb', line 116

def destroy
  @thing.destroy

  flash[:notice] = "#{thing} #{@thing.id} was successfully destroyed."
  redirect_to action: :index
end

#edit(options = { only: [], except: [:id, :created_at, :updated_at], required: [] }) ⇒ Object

GET /:things/edit

options - hash of options

only - filter attributes to edit by whitelist
except - filter attributes to edit by blacklist (has some preset defaults that you probably don't want)


81
82
83
84
85
86
87
88
89
# File 'app/controllers/admin/admin_controller.rb', line 81

def edit(options = { only: [], except: [:id, :created_at, :updated_at], required: [] })
  @required_keys = options[:required]
  @attrs = CEO::Iterator.filter(@thing.attributes.keys, options)
  @model = @thing.model_name.name.constantize
  @route_name ||= @controller_name ||= controller_name
  @index_route = send(:"admin_#{@route_name}_path")

  render 'admin/edit'
end

#index(options = {}) ⇒ Object

GET /things Public: Indexes all things in the model.

Uses pagination by default.

Sets several variables and renders to a view.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/admin/admin_controller.rb', line 17

def index(options = {})
  options[:query] ||= []
  @page = (params[:page] || 1).to_i
  @route_name ||= @controller_name ||= controller_name

  @iterator = CEO::Iterator.new(
    thing,
    query: options[:query],
    page: @page,
    per_page: options.fetch(:per_page, 20),
    filters: {
      only: options[:only],
      except: options[:except]
    }
  )

  @things = @iterator.all || []
  @total_pages = @iterator.total_pages

  @model_name ||= thing.to_s.underscore
  @human_model = @model_name.humanize
  @title = thing.to_s.titleize.pluralize
  render 'admin/index'
end

#new(options = { only: [], except: [:id, :created_at, :updated_at], required: [] }) ⇒ Object

GET /:things/new



66
67
68
69
70
71
72
73
74
# File 'app/controllers/admin/admin_controller.rb', line 66

def new(options = { only: [], except: [:id, :created_at, :updated_at], required: [] })
  @thing_model = thing
  @thing = @thing_model.new
  @attrs = CEO::Iterator.filter(@thing.attributes.keys, options)
  @route_name ||= @controller_name ||= controller_name
  @index_route = send(:"admin_#{@route_name}_path")

  render 'admin/new'
end

#show(options = {}) ⇒ Object

GET /:things/:id



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/admin/admin_controller.rb', line 43

def show(options = {})
  @model = thing
  @title = @model.to_s.titleize.pluralize
  if options[:query]
    query_out = {}
    iterator = CEO::Iterator.new(thing)
    options[:query].each do |q|
      query_out.merge! iterator.query_eval(@thing, q)
    end
  end

  filtered_keys = CEO::Iterator.filter(keys(@thing), options)
  @thing_attrs = @thing.attributes.select do |k, _|
    filtered_keys.include? k
  end

  @thing_attrs.transform_keys! { |k| CEO::Iterator.acronymize(k) }
  @thing_attrs.merge! query_out if query_out

  render 'admin/show'
end

#updateObject

PATCH/PUT /:things/:id



105
106
107
108
109
110
111
112
113
# File 'app/controllers/admin/admin_controller.rb', line 105

def update
  @route_name ||= @controller_name ||= controller_name

  if @thing.update(thing_params)
    redirect_to things_path(@route_name), notice: "#{@controller_name.titleize} successfully updated."
  else
    render 'admin/edit'
  end
end