Class: ActiveElement::DefaultController::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/active_element/default_controller/controller.rb

Overview

Encapsulation of all logic performed for default controller actions when no action is defined by the current controller.

Instance Method Summary collapse

Constructor Details

#initialize(controller:) ⇒ Controller

rubocop:disable Metrics/ClassLength



8
9
10
# File 'lib/active_element/default_controller/controller.rb', line 8

def initialize(controller:)
  @controller = controller
end

Instance Method Details

#createObject

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/active_element/default_controller/controller.rb', line 35

def create # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  return render_forbidden(:editable) unless configured?(:editable)

  new_record = model.new(default_record_params.params)
  # XXX: Ensure associations are applied - there must be a better way.
  if new_record.save && new_record.reload.update(default_record_params.params)
    controller.flash.notice = "#{new_record.model_name.to_s.titleize} created successfully."
    controller.redirect_to record_path(new_record, :show).path
  else
    controller.flash.now.alert = "Failed to create #{model.name.to_s.titleize}."
    controller.render 'active_element/default_views/new', locals: { record: new_record, namespace: namespace }
  end
rescue ActiveRecord::RangeError => e
  render_range_error(error: e, action: :new)
end

#destroyObject



71
72
73
74
75
76
77
# File 'lib/active_element/default_controller/controller.rb', line 71

def destroy
  return render_forbidden(:deletable) unless configured?(:deletable)

  record.destroy
  controller.flash.notice = "Deleted #{record.model_name.to_s.titleize}."
  controller.redirect_to record_path(model, :index).path
end

#editObject



51
52
53
54
55
# File 'lib/active_element/default_controller/controller.rb', line 51

def edit
  return render_forbidden(:editable) unless configured?(:editable)

  controller.render 'active_element/default_views/edit', locals: { record: record, namespace: namespace }
end

#indexObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/active_element/default_controller/controller.rb', line 12

def index
  return render_forbidden(:listable) unless configured?(:listable)

  controller.render 'active_element/default_views/index',
                    locals: {
                      collection: ordered(collection),
                      search_filters: default_text_search.search_filters,
                      nested_for: nested_relations
                    }
end

#newObject



29
30
31
32
33
# File 'lib/active_element/default_controller/controller.rb', line 29

def new
  return render_forbidden(:editable) unless configured?(:editable)

  controller.render 'active_element/default_views/new', locals: { record: model.new, namespace: namespace }
end

#showObject



23
24
25
26
27
# File 'lib/active_element/default_controller/controller.rb', line 23

def show
  return render_forbidden(:viewable) unless configured?(:viewable)

  controller.render 'active_element/default_views/show', locals: { record: record }
end

#updateObject

rubocop:disable Metrics/AbcSize



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/active_element/default_controller/controller.rb', line 57

def update # rubocop:disable Metrics/AbcSize
  return render_forbidden(:editable) unless configured?(:editable)

  if record.update(default_record_params.params)
    controller.flash.notice = "#{record.model_name.to_s.titleize} updated successfully."
    controller.redirect_to record_path(record, :show).path
  else
    controller.flash.now.alert = "Failed to update #{model.name.to_s.titleize}."
    controller.render 'active_element/default_views/edit', locals: { record: record, namespace: namespace }
  end
rescue ActiveRecord::RangeError => e
  render_range_error(error: e, action: :edit)
end