Module: Plutonium::Resource::Controllers::CrudActions

Extended by:
ActiveSupport::Concern
Includes:
IndexAction
Included in:
Plutonium::Resource::Controller
Defined in:
lib/plutonium/resource/controllers/crud_actions.rb,
lib/plutonium/resource/controllers/crud_actions/index_action.rb

Defined Under Namespace

Modules: IndexAction

Instance Method Summary collapse

Instance Method Details

#createObject

POST /resources(.format)



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
73
74
75
76
# File 'lib/plutonium/resource/controllers/crud_actions.rb', line 48

def create
  authorize_current! resource_class
  set_page_title "Create #{resource_class.model_name.human.titleize}"

  @resource_record = resource_class.new resource_params

  respond_to do |format|
    if params[:pre_submit]
      format.any(:html, :turbo_stream) { render :new, formats: [:html], status: :unprocessable_content }
    elsif resource_record!.save
      format.any(:html, :turbo_stream) do
        redirect_to redirect_url_after_submit,
          notice: "#{resource_class.model_name.human} was successfully created."
      end
      format.any do
        @current_policy = nil # Reset cached policy so it uses the instance instead of class
        render :show,
          status: :created,
          location: redirect_url_after_submit
      end
    else
      format.any(:html, :turbo_stream) { render :new, formats: [:html], status: :unprocessable_content }
      format.any do
        @errors = resource_record!.errors
        render "errors", status: :unprocessable_content
      end
    end
  end
end

#destroyObject

DELETE /resources/1(.format)



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/plutonium/resource/controllers/crud_actions.rb', line 118

def destroy
  authorize_current! resource_record!

  respond_to do |format|
    resource_record!.destroy

    format.any(:html, :turbo_stream) do
      redirect_to redirect_url_after_destroy,
        notice: "#{resource_class.model_name.human} was successfully deleted."
    end
    format.json { head :no_content }
  rescue ActiveRecord::InvalidForeignKey
    format.any(:html, :turbo_stream) do
      redirect_to resource_url_for(resource_record!),
        alert: "#{resource_class.model_name.human} is referenced by other records."
    end
    format.any do
      @errors = ActiveModel::Errors.new resource_record!
      @errors.add :base,
        :existing_references,
        message: "is referenced by other records"

      render "errors", status: :unprocessable_content
    end
  end
end

#editObject

GET /resources/1/edit



79
80
81
82
83
84
85
86
# File 'lib/plutonium/resource/controllers/crud_actions.rb', line 79

def edit
  authorize_current! resource_record!
  set_page_title "Update #{resource_record!.to_label.titleize}"

  

  render :edit, formats: [:html]
end

#indexObject

GET /resources(.format)



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/plutonium/resource/controllers/crud_actions.rb', line 13

def index
  authorize_current! resource_class
  set_page_title resource_class.model_name.human.pluralize.titleize

  setup_index_action!

  respond_to do |format|
    format.any(:html, :turbo_stream) { render :index, formats: [:html] }
    format.any { render :index }
  end
end

#newObject

GET /resources/new



37
38
39
40
41
42
43
44
45
# File 'lib/plutonium/resource/controllers/crud_actions.rb', line 37

def new
  authorize_current! resource_class
  set_page_title "Create #{resource_class.model_name.human.titleize}"

  @resource_record = resource_class.new
  

  render :new, formats: [:html]
end

#showObject

GET /resources/1(.format)



26
27
28
29
30
31
32
33
34
# File 'lib/plutonium/resource/controllers/crud_actions.rb', line 26

def show
  authorize_current! resource_record!
  set_page_title resource_record!.to_label.titleize

  respond_to do |format|
    format.any(:html, :turbo_stream) { render :show, formats: [:html] }
    format.any { render :show }
  end
end

#updateObject

PATCH/PUT /resources/1(.format)



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/plutonium/resource/controllers/crud_actions.rb', line 89

def update
  authorize_current! resource_record!
  set_page_title "Update #{resource_record!.to_label.titleize}"

  resource_record!.attributes = resource_params

  respond_to do |format|
    if params[:pre_submit]
      format.any(:html, :turbo_stream) { render :edit, formats: [:html], status: :unprocessable_content }
    elsif resource_record!.save
      format.any(:html, :turbo_stream) do
        redirect_to redirect_url_after_submit,
          notice: "#{resource_class.model_name.human} was successfully updated.",
          status: :see_other
      end
      format.any do
        render :show, status: :ok, location: redirect_url_after_submit
      end
    else
      format.any(:html, :turbo_stream) { render :edit, formats: [:html], status: :unprocessable_content }
      format.any do
        @errors = resource_record!.errors
        render "errors", status: :unprocessable_content
      end
    end
  end
end