Class: Trestle::Resource::Controller

Inherits:
Admin::Controller show all
Defined in:
lib/trestle/resource/controller.rb

Instance Method Summary collapse

Methods inherited from Admin::Controller

#admin

Methods included from Controller::Location

#set_trestle_location_header, #visit_location_with_turbolinks

Instance Method Details

#createObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/trestle/resource/controller.rb', line 27

def create
  self.instance = admin.build_instance(admin.permitted_params(params), params)

  if admin.save_instance(instance, params)
    respond_to do |format|
      format.html do
        flash[:message] = flash_message("create.success", title: "Success!", message: "The %{lowercase_model_name} was successfully created.")
        redirect_to_return_location(:create, instance, default: admin.instance_path(instance))
      end
      format.json { render json: instance, status: :created, location: admin.instance_path(instance) }

      yield format if block_given?
    end
  else
    respond_to do |format|
      format.html do
        flash.now[:error] = flash_message("create.failure", title: "Warning!", message: "Please correct the errors below.")
        render "new", status: :unprocessable_entity
      end
      format.json { render json: instance.errors, status: :unprocessable_entity }

      yield format if block_given?
    end
  end
end

#destroyObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/trestle/resource/controller.rb', line 115

def destroy
  success = admin.delete_instance(instance, params)

  respond_to do |format|
    format.html do
      if success
        flash[:message] = flash_message("destroy.success", title: "Success!", message: "The %{lowercase_model_name} was successfully deleted.")
        redirect_to_return_location(:destroy, instance, default: admin.path(:index))
      else
        flash[:error] = flash_message("destroy.failure", title: "Warning!", message: "Could not delete %{lowercase_model_name}.")

        if self.instance = admin.find_instance(params)
          redirect_to_return_location(:update, instance, default: admin.instance_path(instance))
        else
          redirect_to_return_location(:destroy, instance, default: admin.path(:index))
        end
      end
    end
    format.json { head :no_content }

    yield format if block_given?
  end
end

#editObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/trestle/resource/controller.rb', line 71

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.json { render json: instance }

      yield format if block_given?
    end
  end
end

#indexObject



7
8
9
10
11
12
13
14
# File 'lib/trestle/resource/controller.rb', line 7

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

    yield format if block_given?
  end
end

#newObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/trestle/resource/controller.rb', line 16

def new
  self.instance = admin.build_instance(params.key?(admin.parameter_name) ? admin.permitted_params(params) : {}, params)

  respond_to do |format|
    format.html
    format.json { render json: instance }

    yield format if block_given?
  end
end

#showObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/trestle/resource/controller.rb', line 53

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.json { render json: instance }

      yield format if block_given?
    end
  end
end

#updateObject



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
# File 'lib/trestle/resource/controller.rb', line 89

def update
  admin.update_instance(instance, admin.permitted_params(params), params)

  if admin.save_instance(instance, params)
    respond_to do |format|
      format.html do
        flash[:message] = flash_message("update.success", title: "Success!", message: "The %{lowercase_model_name} was successfully updated.")
        redirect_to_return_location(:update, instance, default: admin.instance_path(instance))
      end
      format.json { render json: instance, status: :ok }

      yield format if block_given?
    end
  else
    respond_to do |format|
      format.html do
        flash.now[:error] = flash_message("update.failure", title: "Warning!", message: "Please correct the errors below.")
        render "show", status: :unprocessable_entity
      end
      format.json { render json: instance.errors, status: :unprocessable_entity }

      yield format if block_given?
    end
  end
end