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
77
78
79
80
81
82
# 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.turbo_stream { render turbo_stream: turbo_stream.replace("resource-form", view_context.render(build_form(action: :new))) }
      format.html { render :new, status: :unprocessable_content }
    elsif resource_record!.save
      format.turbo_stream do
        flash.notice = "#{resource_class.model_name.human} was successfully created."
        render turbo_stream: helpers.turbo_stream_redirect(redirect_url_after_submit)
      end
      format.html 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.turbo_stream { render turbo_stream: turbo_stream.replace("resource-form", view_context.render(build_form(action: :new))), status: :unprocessable_content }
      format.html { render :new, status: :unprocessable_content }
      format.any do
        @errors = resource_record!.errors
        render "errors", status: :unprocessable_content
      end
    end
  end
end

#destroyObject

DELETE /resources/1(.format)



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/plutonium/resource/controllers/crud_actions.rb', line 130

def destroy
  authorize_current! resource_record!

  respond_to do |format|
    resource_record!.destroy

    format.turbo_stream do
      flash.notice = "#{resource_class.model_name.human} was successfully deleted."
      render turbo_stream: helpers.turbo_stream_redirect(redirect_url_after_destroy)
    end
    format.html 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.turbo_stream do
      flash.alert = "#{resource_class.model_name.human} is referenced by other records."
      render turbo_stream: helpers.turbo_stream_redirect(resource_url_for(resource_record!))
    end
    format.html 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



85
86
87
88
89
90
91
92
# File 'lib/plutonium/resource/controllers/crud_actions.rb', line 85

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 helpers.nestable_resource_name_plural(resource_class)

  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)



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/plutonium/resource/controllers/crud_actions.rb', line 95

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.turbo_stream { render turbo_stream: turbo_stream.replace("resource-form", view_context.render(build_form(action: :edit))) }
      format.html { render :edit, status: :unprocessable_content }
    elsif resource_record!.save
      format.turbo_stream do
        flash.notice = "#{resource_class.model_name.human} was successfully updated."
        render turbo_stream: helpers.turbo_stream_redirect(redirect_url_after_submit)
      end
      format.html 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.turbo_stream { render turbo_stream: turbo_stream.replace("resource-form", view_context.render(build_form(action: :edit))), status: :unprocessable_content }
      format.html { render :edit, status: :unprocessable_content }
      format.any do
        @errors = resource_record!.errors
        render "errors", status: :unprocessable_content
      end
    end
  end
end