-
Notifications
You must be signed in to change notification settings - Fork 92
/
rapid_editing.dryml
226 lines (188 loc) · 8.45 KB
/
rapid_editing.dryml
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<def tag="editor" ><%=
if !can_edit?
view(attributes)
elsif (refl = this_field_reflection)
if refl.macro == :belongs_to
belongs_to_editor(attributes)
else
has_many_editor(attributes)
end
else
attrs = add_classes(attributes, type_id, type_and_field, "editor")
call_polymorphic_tag("editor", attrs) or
raise HoboError.new("<editor> not implemented for #{this.class.name}\##{this_field} " +
"(#{this.inspect}:#{this_type})")
end
%></def>
<def tag="has-many-editor">
<% #TODO: Implement %>
<a merge-attrs/>
</def>
<def tag="belongs-to-editor"><%= belongs_to_menu_editor(attributes) %></def>
<def tag="editor" for="string"><%= in_place_editor "in_place_textfield_bhv", attributes %></def>
<def tag="editor" for="text"><%= in_place_editor "in_place_textarea_bhv", attributes %></def>
<def tag="editor" for="html"><%= in_place_editor "in_place_html_textarea_bhv", attributes %></def>
<def tag="editor" for="datetime"><%= in_place_editor "in_place_textfield_bhv", attributes %></def>
<def tag="editor" for="date"><%= in_place_editor "in_place_textfield_bhv", attributes %></def>
<def tag="editor" for="integer"><%= in_place_editor "in_place_textfield_bhv", attributes %></def>
<def tag="editor" for="float"><%= in_place_editor "in_place_textfield_bhv", attributes %></def>
<def tag="editor" for="password"><% raise HoboError, "passwords cannot be edited in place" %></def>
<def tag="editor" for="boolean"><boolean-checkbox-editor merge-attrs/></def>
<def tag="editor" for="big_integer"><%= in_place_editor "in_place_textfield_bhv", attributes %></def>
<def tag="editor" for="HoboFields::EnumString">
<string-select-editor values="&this_type.values" merge/>
</def>
<def tag="autocompleter" attrs="completer-model, completer-attr, id, filter, name, value">
<% refl = this_type
if refl.respond_to?(:macro) && refl.macro == :belongs_to
completer_model ||= refl.klass
completer_attr ||= refl.klass.id_name_column
else
completer_model = completer_model.constantize if completer_model.is_a? String
end
id ||= dom_id + "_completer"
url = object_url(completer_model, "completions",
{ :for => completer_attr }.update(attributes.select_hash {|k,v| k.to_s.starts_with? "where_"}))
%>
<input type="text" name="#{name}" id="#{id}" class="autocomplete-bhv"
autocomplete-url="#{url}" value="#{value}"
merge-attrs/>
<div id="#{id}-completions" class="completions-popup" style="display:none"></div>
</def>
<def tag="belongs-to-menu-editor" attrs="sort"><%
raise HoboError.new("Not allowed to edit") unless can_edit?
link_id = "#{dom_id}_editor" %>
<span id="#{link_id}" part="rapid-belongs-to-edit" part-locals="sort">
<% select_options = this_field_reflection.klass.find(:all).select {|x| can_view?(x)}.map {|x|
[ name(:with => x, :no_wrapper => true), x.id ]
}
select_options = select_options.sort if sort
select_options.insert(0, ["(No #{this_type.name.to_s.titleize})", ""]) if this.nil?
link_id = "#{dom_id}_editor"
f = ajax_updater(object_url(this_parent),
[link_id],
:method => "put",
:params => { this_parent.class.name.underscore => {
this_field_reflection.primary_key_name => Hobo.raw_js('this.value')
} })
%>
<select onchange="#{f}">
<%= options_for_select(select_options, this ? this.id : "") %>
</select>
</span>
</def>
<def tag="belongs-to-autocompleting-editor" attrs="update">
<if-can-edit><%
return object_link unless can_edit?
id ||= dom_id + "_completer"
f = ajax_updater(object_url(this_parent),
update,
:method => "put",
:params => { this_parent.class.name.underscore => {
this_field => Hobo.raw_js("$('#{id}').value")
} })
refl = this_type
completer_model ||= refl.klass
completer_attr ||= refl.klass.id_name_column
url = object_url(completer_model, "completions",
{ :for => completer_attr },
attributes.select_hash {|k,v| k.to_s.starts_with? "where_"})
%>
<form onsubmit="#{f}; $('#{id}').blur(); return false">
<input type="text" class="autocomplete-bhv autosubmit" id="#{id}" autocomplete-url="#{url}"
value="#{this && this.id_name}" merge-attrs />
<div id="#{id}-completions" class="completions-popup" style="display:none"></div>
</form>
</if-can-edit>
<else>
<object-link/>
</else>
</def>
<def tag="string-select-editor" attrs="update, values"><%
raise HoboError.new("Not allowed to edit") unless can_edit?
values = comma_split(values)
f = ajax_updater(object_url(this_parent),
update,
:method => "put",
:params => { this_parent.class.name.underscore => {
this_field => Hobo.raw_js('this.value')
} })
html_attributes = add_classes(attributes, editor_class)
%>
<select onchange="#{f}" merge-attrs="&html_attributes">
<%= options_for_select(values, this) %>
</select>
</def>
<def tag="boolean-checkbox-editor" attrs="update, message"><%
raise HoboError.new("Not allowed to edit") unless can_edit?
f = ajax_updater(object_url(this_parent),
update,
:method => "put",
:message => message,
:spinner_next_to => Hobo.raw_js("this"),
:params => { this_parent.class.name.underscore => {
this_field => Hobo.raw_js('this.checked')
} })
attributes = add_classes(attributes, editor_class)
%>
<input type="checkbox" value="1" onclick="#{f}"
merge-attrs="& this ? attributes.merge(:checked => 'checked') : attributes" />
</def>
<def tag="sti-type-editor" attrs="update">
<% base_class = this.class
base_class = base_class.superclass while base_class.superclass != ActiveRecord::Base
f = ajax_updater("#{base_url}/#{controller_for base_class}/#{this.id}",
update,
:method => "put",
:params => { base_class.name.underscore => {
"type" => Hobo.raw_js('this.value')
} })
%>
<select onchange="#{f}">
<%= parameters.default || options_for_select(base_class.send(:subclasses).map{|x| [x.name.titleize, x.name]},
this.class.name) %>
</select>
</def>
<def tag="integer-select-editor" attrs="options, min, max, update, nil-option, message">
<% options ||= (min.to_i..max.to_i).to_a %>
<select class="number-editor-bhv" hobo-update="#{update}"
hobo-model-id="#{dom_id}"
merge-attrs="&message ? attributes.merge(:hobo_message => message) : attributes">
<if test="&this.nil?"><option value=""><%= nil_option || "Choose a value" %></option></if>
<%= options_for_select(options, this) %>
</select>
</def>
<def tag="has-many-checkbox-editor" attrs="model, update, message"><%=
raise HoboError.new("no update specified") unless update
fields = attributes.delete_if{|k,v|!k.ends_with? "_id"}
conditions = fields.map{|k,v|"#{k}=#{v}"}.join " AND "
klass = model.is_a?(String) ? model.constantize : model
obj = klass.find(:first, :conditions => conditions)
checkbox_attrs = {:type =>'checkbox'}
if obj == nil
new = klass.new(fields)
permission = if can_create?(new)
class_name = new.class.name.underscore
ajax_options = { :message => message }
ajax_options[:params] = { class_name => fields } unless fields.empty?
checkbox_attrs[:onclick] = ajax_updater(object_url(new.class), update, ajax_options)
end
else
permission = if can_delete?(obj)
checkbox_attrs[:checked] = 'checked'
message ||= "Unsetting #{obj.class.name.titleize}"
class_name = obj.class.name.underscore
checkbox_attrs[:onclick] = ajax_updater(object_url(obj, "destroy"), update, {:message => message, :method => 'delete'})
end
end
element(:input, add_classes(attributes.merge(checkbox_attrs),
"checkbox_input has_many_checkbox has_many_#{class_name}_checkbox")) if permission
%></def>
<def tag="has-many-checkbox-editors">
<table>
<tr:>
<td><has-many-checkbox-editor param="editor" merge-attrs/></td>
<td><name param/></td>
</tr>
</table>
</def>