Class: AbstractResourcesController

Inherits:
ApplicationController show all
Includes:
ParentControl, PrintControl, ResourceControl
Defined in:
app/controllers/abstract_resources_controller.rb

Instance Method Summary collapse

Methods included from ParentControl

#find_parent, #parent, #parent?, #parent_class, #parent_class=, #parent_url, #recognise_parent, #recognise_path, #recognise_resource, #set_parents, #update_parenthood

Methods included from ResourceControl

#_filter, #_flip, #_id, #_paginate, #_search, #_sort, #find_all_resources, #find_resources, #find_resources_queried, #new_resource, #resource, #resource?, #resource_class, #resource_class=, #resource_name, #resource_options, #resource_params, #resource_url, #resources, #resources_url, #set_resource, #set_resources, #sorting_direction

Methods included from PrintControl

#display_print_result, #download_print_result, #email_print_result, #print, #print_print_result, #print_resources

Methods inherited from ApplicationController

#user_not_authorized

Instance Method Details

#activateObject



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/abstract_resources_controller.rb', line 51

def activate
  authorize resource
  if resource.activate
    flash[:info] = t(:resource_activated_correct)
    render :activate, layout: false, status: 200 and return
  else
    flash[:error] = t(:resource_activated_incorrect)
    render :activate, layout: false, status: 401 and return
  end

rescue Exception => e
  scoop_from_error e
end

#attachObject



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/abstract_resources_controller.rb', line 80

def attach
  authorize resource
  if resource.attach parent
    flash[:info] = t(:resource_attached_correct)
    render :attach, layout: false, status: 200 and return
  else
    flash[:error] = t(:resource_attached_incorrect)
    render :attach, layout: false, status: 401 and return
  end

rescue Exception => e
  scoop_from_error e
end

#createObject



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/controllers/abstract_resources_controller.rb', line 137

def create
  authorize resource
  respond_with(resource, location: redirect_after_create ) do |format|
    if resource.save && update_parenthood
      flash[:notice] = t('.success.created', resource: resource_class.to_s )
    else
      format.html { render action: :new, status: :unprocessable_entity }
      format.js { render action: :new, status: :unprocessable_entity }
    end
  end
rescue Exception => e
  scoop_from_error e
end

#deactivateObject



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/abstract_resources_controller.rb', line 65

def deactivate
  authorize resource
  if resource.deactivate
    flash[:info] = t(:resource_deactivated_correct)
    render :deactivate, layout: false, status: 200 and return
  else
    flash[:error] = t(:resource_deactivated_incorrect)
    render :deactivate, layout: false, status: 401 and return
  end

rescue Exception => e
  scoop_from_error e
end

#deferObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/abstract_resources_controller.rb', line 37

def defer
  authorize resource
  if resource.defer parent
    flash[:info] = t(:resource_deferred_correct)
    render :defer, layout: false, status: 200 and return
  else
    flash[:error] = t(:resource_deferred_incorrect)
    render :defer, layout: false, status: 401 and return
  end

rescue Exception => e
  scoop_from_error e
end

#destroyObject



165
166
167
168
169
170
171
172
173
174
175
176
# File 'app/controllers/abstract_resources_controller.rb', line 165

def destroy
  authorize resource
  result = true if resource.destroy && update_parenthood
  result ? (flash.now[:notice] = t('.success', resource: resource_class.to_s)) : (flash.now[:error] = t('.deleted.error',resource: resource_class.to_s))
  if result==true
    render layout:false, status: 200, locals: { result: true }
  else
    render layout:false, status: 301, locals: { result: true }
  end
rescue Exception => e
  scoop_from_error e
end

#detachObject



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/controllers/abstract_resources_controller.rb', line 94

def detach
  authorize resource
  if resource.detach parent
    flash[:info] = t(:resource_detached_correct)
    render :detach, layout: false, status: 200 and return
  else
    flash[:error] = t(:resource_detached_incorrect)
    render :detach, layout: false, status: 401 and return
  end

rescue Exception => e
  scoop_from_error e
end

#editObject



123
124
125
126
127
128
# File 'app/controllers/abstract_resources_controller.rb', line 123

def edit
  authorize resource
  respond_with resource
rescue Exception => e
  scoop_from_error e
end

#indexObject



130
131
132
133
134
135
# File 'app/controllers/abstract_resources_controller.rb', line 130

def index
  authorize resource_class, :index?
  respond_with resources
rescue Exception => e
  scoop_from_error e
end

#newObject



115
116
117
118
119
120
121
# File 'app/controllers/abstract_resources_controller.rb', line 115

def new
  resource.parent_id = params[:parent_id] if resource.respond_to? :parent_id
  authorize resource
  respond_with resource
rescue Exception => e
  scoop_from_error e
end

#preferObject

default implementation



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/abstract_resources_controller.rb', line 23

def prefer
  authorize resource
  if resource.prefer parent
    flash[:info] = t(:resource_preferred_correct)
    render :prefer, layout: false, status: 200 and return
  else
    flash[:error] = t(:resource_preferred_incorrect)
    render :prefer, layout: false, status: 401 and return
  end

rescue Exception => e
  scoop_from_error e
end

#showObject



108
109
110
111
112
113
# File 'app/controllers/abstract_resources_controller.rb', line 108

def show
  authorize resource
  respond_with resource
rescue Exception => e
  scoop_from_error e
end

#updateObject



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/controllers/abstract_resources_controller.rb', line 151

def update
  authorize resource
  respond_with(resource, location: redirect_after_update) do |format|
    if resource.update_attributes(resource_params) && update_parenthood
      flash[:notice] = t('.success.updated', resource: resource_class.to_s )
    else
      format.html { render action: :edit, status: :unprocessable_entity }
      format.js { render action: :edit, status: :unprocessable_entity }
    end
  end
rescue Exception => e
  scoop_from_error e
end