Class: Admin::CEOController

Inherits:
AdminController show all
Defined in:
app/controllers/admin/ceo_controller.rb

Overview

Public: A base controller for admin pages.

This class is the meat of CEO. It contains the helper methods and pages that make CEO what it is.

Instance Method Summary collapse

Methods inherited from AdminController

#authenticate_admin!, #dashboard, #styleguide

Instance Method Details

#createObject

POST /:things



96
97
98
99
100
101
102
103
104
105
106
# File 'app/controllers/admin/ceo_controller.rb', line 96

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



121
122
123
124
125
126
# File 'app/controllers/admin/ceo_controller.rb', line 121

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)


85
86
87
88
89
90
91
92
93
# File 'app/controllers/admin/ceo_controller.rb', line 85

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.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/admin/ceo_controller.rb', line 21

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],
    current_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



70
71
72
73
74
75
76
77
78
# File 'app/controllers/admin/ceo_controller.rb', line 70

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/admin/ceo_controller.rb', line 47

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



109
110
111
112
113
114
115
116
117
118
# File 'app/controllers/admin/ceo_controller.rb', line 109

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
    @model = @thing.model_name.name.constantize
    render 'admin/edit'
  end
end