-
Notifications
You must be signed in to change notification settings - Fork 17
/
resources_controller.rb
148 lines (117 loc) · 3.49 KB
/
resources_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
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
83
84
85
86
87
88
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
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
141
142
143
144
145
146
147
148
module MailyHerald
class Webui::ResourcesController < Webui::ApplicationController
include SmartListing::Helper::ControllerExtensions
helper SmartListing::Helper
helper_method :resource_spec
before_filter :find_item, :only => [:show, :edit, :update, :destroy]
class Spec
DEFAULT_CONTAINER = "details"
attr_accessor :klass, :scope, :filter_proc, :items_partial, :item_partial, :locale_prefix, :params, :containers_order
def filter scope, query
@filter_proc.call(scope, query)
end
def update_containers
@update_containers ||= {
DEFAULT_CONTAINER => {editable: true}
}
end
def update_containers= containers
update_containers.merge!(containers)
end
end
def index
scope = resource_spec.scope
scope = resource_spec.filter(scope, params[:filter]) if params[:filter]
if block_given?
scope = yield(scope)
end
@items = smart_listing_create(:items, scope, :partial => resource_spec.items_partial || "items")
end
def new
add_breadcrumb view_context.tw("#{controller_name}.new.label")
@item = resource_spec.klass.new
end
def create
@item = resource_spec.klass.new
@item.attributes = item_params
yield(@item) if block_given?
if @item.save
if @item.is_a?(MailyHerald::SequenceMailing)
redirect_to sequence_mailing_path(@item.sequence, @item)
else
redirect_to @item
end
else
add_breadcrumb view_context.tw("#{controller_name}.new.label", :action => :new)
render :action => :new
end
end
def show
render_containers resource_spec.update_containers.keys
end
def edit
set_edited_container
end
def update
@item.update_attributes(item_params)
render_containers resource_spec.update_containers.keys
set_edited_container
end
def destroy
@item.destroy
flash[:notice] = "destroyed"
redirect_to :action => :index
end
protected
def resource_spec
@resource_spec ||= set_resource_spec
end
private
# Adds containers to list of containers to be updated (reloaded)
def render_containers containers, options = {}
containers = [*containers]
if options[:append]
@rendered_containers ||= []
@rendered_containers.concat(containers.flatten)
else
@rendered_containers = containers
end
end
# Sets main container which is editable
def set_edited_container name = nil
@edited_container = name || params[:edited_container] || Spec::DEFAULT_CONTAINER
end
def edited_container
@edited_container
end
# Renders update template from different action
def render_update
render action: "update"
end
# Collect container dependencies and render
def render(options = {}, locals = {}, &block)
collect_action_dependencies
super
end
def find_item
@item = resource_spec.klass.find params[:id]
end
# Run code required by rendered containers
def collect_action_dependencies
if @rendered_containers
action_dependencies @rendered_containers
#begin
#action_dependencies @rendered_containers
#rescue NoMethodError
#end
end
end
def item_params
if Rails::VERSION::MAJOR == 3
params[:item]
else
params.require(:item).permit(*resource_spec.params)
end
end
end
end