Module: Trestle::Resource::Controller::Actions

Included in:
Trestle::ResourceController
Defined in:
app/controllers/concerns/trestle/resource/controller/actions.rb

Instance Method Summary collapse

Instance Method Details

#createObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/concerns/trestle/resource/controller/actions.rb', line 24

def create
  if save_instance
    respond_to do |format|
      flash[:message] = flash_message("create.success", title: "Success!", message: "The %{lowercase_model_name} was successfully created.")

      format.html { redirect_to_return_location(:create, instance) { admin.instance_path(instance) } }
      format.turbo_stream { flash.discard } if modal_request?
      format.json { render json: instance, status: :created, location: admin.instance_path(instance) }

      yield format if block_given?
    end
  else
    respond_to do |format|
      flash.now[:error] = flash_message("create.failure", title: "Warning!", message: "Please correct the errors below.")

      format.html { render "new", status: :unprocessable_entity }
      format.turbo_stream { render "create", status: :unprocessable_entity } if modal_request?
      format.json { render json: instance.errors, status: :unprocessable_entity }

      yield format if block_given?
    end
  end
end

#destroyObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/controllers/concerns/trestle/resource/controller/actions.rb', line 110

def destroy
  deleting_referer = URI(request.referer).path == admin.instance_path(instance)

  if delete_instance
    respond_to do |format|
      flash[:message] = flash_message("destroy.success", title: "Success!", message: "The %{lowercase_model_name} was successfully deleted.")

      format.html { redirect_to_return_location(:destroy, instance, status: :see_other) { admin.path(:index) } }
      format.turbo_stream { flash.discard } unless deleting_referer
      format.json { head :no_content }

      yield format if block_given?
    end
  else
    respond_to do |format|
      flash[:error] = flash_message("destroy.failure", title: "Warning!", message: "Could not delete %{lowercase_model_name}.")

      format.html do
        if load_instance
          redirect_to_return_location(:update, instance) { admin.instance_path(instance) }
        else
          redirect_to_return_location(:destroy, instance, status: :see_other) { admin.path(:index) }
        end
      end
      format.turbo_stream { flash.discard }
      format.json { head :no_content }

      yield format if block_given?
    end
  end
end

#editObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/concerns/trestle/resource/controller/actions.rb', line 67

def edit
  if admin.singular? && instance.nil?
    respond_to do |format|
      format.html { redirect_to action: :new }
      format.json { head :not_found }

      yield format if block_given?
    end
  else
    respond_to do |format|
      format.html
      format.turbo_stream { render turbo_stream: turbo_stream.modal } if modal_request?
      format.json { render json: instance }

      yield format if block_given?
    end
  end
end

#indexObject



5
6
7
8
9
10
11
12
# File 'app/controllers/concerns/trestle/resource/controller/actions.rb', line 5

def index
  respond_to do |format|
    format.html
    format.json { render json: collection }

    yield format if block_given?
  end
end

#newObject



14
15
16
17
18
19
20
21
22
# File 'app/controllers/concerns/trestle/resource/controller/actions.rb', line 14

def new
  respond_to do |format|
    format.html
    format.turbo_stream { render turbo_stream: turbo_stream.modal }
    format.json { render json: instance }

    yield format if block_given?
  end
end

#showObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/concerns/trestle/resource/controller/actions.rb', line 48

def show
  if admin.singular? && instance.nil?
    respond_to do |format|
      format.html { redirect_to action: :new }
      format.json { head :not_found }

      yield format if block_given?
    end
  else
    respond_to do |format|
      format.html
      format.turbo_stream { render turbo_stream: turbo_stream.modal } if modal_request?
      format.json { render json: instance }

      yield format if block_given?
    end
  end
end

#updateObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/controllers/concerns/trestle/resource/controller/actions.rb', line 86

def update
  if update_instance
    respond_to do |format|
      flash[:message] = flash_message("update.success", title: "Success!", message: "The %{lowercase_model_name} was successfully updated.")

      format.html { redirect_to_return_location(:update, instance) { admin.instance_path(instance) } }
      format.turbo_stream { flash.discard }
      format.json { render json: instance, status: :ok }

      yield format if block_given?
    end
  else
    respond_to do |format|
      flash.now[:error] = flash_message("update.failure", title: "Warning!", message: "Please correct the errors below.")

      format.html { render "show", status: :unprocessable_entity }
      format.turbo_stream { render "update", status: :unprocessable_entity }
      format.json { render json: instance.errors, status: :unprocessable_entity }

      yield format if block_given?
    end
  end
end