fesplugas-typus 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +7 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +80 -0
- data/Rakefile +61 -0
- data/VERSION +1 -0
- data/app/controllers/admin/master_controller.rb +324 -0
- data/app/controllers/typus_controller.rb +127 -0
- data/app/helpers/admin/form_helper.rb +351 -0
- data/app/helpers/admin/master_helper.rb +99 -0
- data/app/helpers/admin/public_helper.rb +24 -0
- data/app/helpers/admin/sidebar_helper.rb +259 -0
- data/app/helpers/admin/table_helper.rb +227 -0
- data/app/helpers/typus_helper.rb +169 -0
- data/app/models/typus_mailer.rb +14 -0
- data/app/models/typus_user.rb +5 -0
- data/app/views/admin/dashboard/_sidebar.html.erb +9 -0
- data/app/views/admin/resources/edit.html.erb +29 -0
- data/app/views/admin/resources/index.html.erb +28 -0
- data/app/views/admin/resources/new.html.erb +27 -0
- data/app/views/admin/resources/show.html.erb +21 -0
- data/app/views/admin/shared/_footer.html.erb +1 -0
- data/app/views/admin/shared/_pagination.html.erb +28 -0
- data/app/views/layouts/admin.html.erb +72 -0
- data/app/views/layouts/typus.html.erb +29 -0
- data/app/views/typus/dashboard.html.erb +13 -0
- data/app/views/typus/recover_password.html.erb +7 -0
- data/app/views/typus/reset_password.html.erb +13 -0
- data/app/views/typus/sign_in.html.erb +9 -0
- data/app/views/typus/sign_up.html.erb +7 -0
- data/app/views/typus_mailer/reset_password_link.erb +11 -0
- data/config/locales/es.yml +106 -0
- data/config/locales/pt-BR.yml +108 -0
- data/config/locales/typus_hacks.yml +14 -0
- data/config/routes.rb +14 -0
- data/generators/typus/templates/config/initializers/typus.rb +27 -0
- data/generators/typus/templates/config/typus/application.yml +45 -0
- data/generators/typus/templates/config/typus/application_roles.yml +23 -0
- data/generators/typus/templates/config/typus/typus.yml +14 -0
- data/generators/typus/templates/config/typus/typus_roles.yml +2 -0
- data/generators/typus/templates/db/create_typus_users.rb +21 -0
- data/generators/typus/templates/public/images/admin/arrow_down.gif +0 -0
- data/generators/typus/templates/public/images/admin/arrow_up.gif +0 -0
- data/generators/typus/templates/public/images/admin/spinner.gif +0 -0
- data/generators/typus/templates/public/images/admin/status_false.gif +0 -0
- data/generators/typus/templates/public/images/admin/status_true.gif +0 -0
- data/generators/typus/templates/public/images/admin/trash.gif +0 -0
- data/generators/typus/templates/public/javascripts/admin/application.js +14 -0
- data/generators/typus/templates/public/stylesheets/admin/reset.css +68 -0
- data/generators/typus/templates/public/stylesheets/admin/screen.css +709 -0
- data/generators/typus/typus_generator.rb +141 -0
- data/generators/typus_update_schema_to_01/templates/config/typus.yml +14 -0
- data/generators/typus_update_schema_to_01/templates/migration.rb +11 -0
- data/generators/typus_update_schema_to_01/typus_update_schema_to_01_generator.rb +19 -0
- data/init.rb +19 -0
- data/lib/typus/active_record.rb +298 -0
- data/lib/typus/authentication.rb +155 -0
- data/lib/typus/configuration.rb +92 -0
- data/lib/typus/format.rb +56 -0
- data/lib/typus/generator.rb +173 -0
- data/lib/typus/hash.rb +10 -0
- data/lib/typus/locale.rb +17 -0
- data/lib/typus/object.rb +22 -0
- data/lib/typus/quick_edit.rb +33 -0
- data/lib/typus/reloader.rb +17 -0
- data/lib/typus/string.rb +11 -0
- data/lib/typus/user.rb +137 -0
- data/lib/typus.rb +133 -0
- data/lib/vendor/active_record.rb +15 -0
- data/lib/vendor/paginator.rb +143 -0
- data/tasks/typus_tasks.rake +26 -0
- data/test/config/broken/application.yml +68 -0
- data/test/config/broken/application_roles.yml +20 -0
- data/test/config/broken/empty.yml +0 -0
- data/test/config/broken/empty_roles.yml +0 -0
- data/test/config/broken/undefined.yml +3 -0
- data/test/config/broken/undefined_roles.yml +6 -0
- data/test/config/default/typus.yml +14 -0
- data/test/config/default/typus_roles.yml +2 -0
- data/test/config/empty/empty_01.yml +0 -0
- data/test/config/empty/empty_01_roles.yml +0 -0
- data/test/config/empty/empty_02.yml +0 -0
- data/test/config/empty/empty_02_roles.yml +0 -0
- data/test/config/locales/es.yml +10 -0
- data/test/config/ordered/001_roles.yml +2 -0
- data/test/config/ordered/002_roles.yml +2 -0
- data/test/config/unordered/app_one_roles.yml +2 -0
- data/test/config/unordered/app_two_roles.yml +2 -0
- data/test/config/working/application.yml +67 -0
- data/test/config/working/application_roles.yml +22 -0
- data/test/config/working/typus.yml +14 -0
- data/test/config/working/typus_roles.yml +2 -0
- data/test/fixtures/app/controllers/admin/assets_controller.rb +2 -0
- data/test/fixtures/app/controllers/admin/categories_controller.rb +2 -0
- data/test/fixtures/app/controllers/admin/comments_controller.rb +2 -0
- data/test/fixtures/app/controllers/admin/pages_controller.rb +2 -0
- data/test/fixtures/app/controllers/admin/posts_controller.rb +2 -0
- data/test/fixtures/app/controllers/admin/status_controller.rb +6 -0
- data/test/fixtures/app/controllers/admin/typus_users_controller.rb +2 -0
- data/test/fixtures/app/controllers/admin/watch_dog_controller.rb +6 -0
- data/test/fixtures/app/views/admin/comments/_edit_bottom.html.erb +1 -0
- data/test/fixtures/app/views/admin/comments/_edit_sidebar.html.erb +1 -0
- data/test/fixtures/app/views/admin/comments/_edit_top.html.erb +1 -0
- data/test/fixtures/app/views/admin/comments/_index_bottom.html.erb +1 -0
- data/test/fixtures/app/views/admin/comments/_index_sidebar.html.erb +1 -0
- data/test/fixtures/app/views/admin/comments/_index_top.html.erb +1 -0
- data/test/fixtures/app/views/admin/comments/_new_bottom.html.erb +1 -0
- data/test/fixtures/app/views/admin/comments/_new_sidebar.html.erb +1 -0
- data/test/fixtures/app/views/admin/comments/_new_top.html.erb +1 -0
- data/test/fixtures/app/views/admin/comments/_show_bottom.html.erb +1 -0
- data/test/fixtures/app/views/admin/comments/_show_sidebar.html.erb +1 -0
- data/test/fixtures/app/views/admin/comments/_show_top.html.erb +1 -0
- data/test/fixtures/app/views/admin/dashboard/_bottom.html.erb +1 -0
- data/test/fixtures/app/views/admin/dashboard/_sidebar.html.erb +1 -0
- data/test/fixtures/app/views/admin/dashboard/_top.html.erb +1 -0
- data/test/fixtures/app/views/admin/shared/_footer.html.erb +1 -0
- data/test/fixtures/app/views/admin/status/index.html.erb +1 -0
- data/test/fixtures/app/views/admin/templates/_datepicker.html.erb +1 -0
- data/test/fixtures/assets.yml +11 -0
- data/test/fixtures/categories.yml +14 -0
- data/test/fixtures/comments.yml +27 -0
- data/test/fixtures/pages.yml +41 -0
- data/test/fixtures/posts.yml +37 -0
- data/test/fixtures/typus_users.yml +54 -0
- data/test/functional/admin/assets_controller_test.rb +57 -0
- data/test/functional/admin/categories_controller_test.rb +106 -0
- data/test/functional/admin/comments_controller_test.rb +121 -0
- data/test/functional/admin/master_controller_test.rb +5 -0
- data/test/functional/admin/posts_controller_test.rb +278 -0
- data/test/functional/admin/status_controller_test.rb +43 -0
- data/test/functional/admin/typus_users_controller_test.rb +239 -0
- data/test/functional/typus_controller_test.rb +315 -0
- data/test/helper.rb +51 -0
- data/test/helpers/admin/form_helper_test.rb +316 -0
- data/test/helpers/admin/master_helper_test.rb +65 -0
- data/test/helpers/admin/public_helper_test.rb +22 -0
- data/test/helpers/admin/sidebar_helper_test.rb +351 -0
- data/test/helpers/admin/table_helper_test.rb +255 -0
- data/test/helpers/typus_helper_test.rb +106 -0
- data/test/lib/active_record_test.rb +372 -0
- data/test/lib/configuration_test.rb +91 -0
- data/test/lib/hash_test.rb +11 -0
- data/test/lib/routes_test.rb +82 -0
- data/test/lib/string_test.rb +25 -0
- data/test/lib/typus_test.rb +105 -0
- data/test/models.rb +51 -0
- data/test/schema.rb +64 -0
- data/test/unit/typus_mailer_test.rb +33 -0
- data/test/unit/typus_test.rb +17 -0
- data/test/unit/typus_user_roles_test.rb +90 -0
- data/test/unit/typus_user_test.rb +177 -0
- data/test/vendor/active_record_test.rb +18 -0
- data/test/vendor/paginator_test.rb +138 -0
- data/typus.gemspec +225 -0
- metadata +241 -0
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
module Admin::FormHelper
|
|
2
|
+
|
|
3
|
+
def build_form(fields)
|
|
4
|
+
|
|
5
|
+
options = { :start_year => @resource[:class].typus_options_for(:start_year),
|
|
6
|
+
:end_year => @resource[:class].typus_options_for(:end_year),
|
|
7
|
+
:minute_step => @resource[:class].typus_options_for(:minute_step) }
|
|
8
|
+
|
|
9
|
+
returning(String.new) do |html|
|
|
10
|
+
html << (error_messages_for :item, :header_tag => 'h3')
|
|
11
|
+
html << '<ul>'
|
|
12
|
+
fields.each do |key, value|
|
|
13
|
+
if template = @resource[:class].typus_template(key)
|
|
14
|
+
html << typus_template_field(key, template, options)
|
|
15
|
+
next
|
|
16
|
+
end
|
|
17
|
+
case value
|
|
18
|
+
when :belongs_to: html << typus_belongs_to_field(key)
|
|
19
|
+
when :boolean: html << typus_boolean_field(key)
|
|
20
|
+
when :date: html << typus_date_field(key, options)
|
|
21
|
+
when :datetime: html << typus_datetime_field(key, options)
|
|
22
|
+
when :file: html << typus_file_field(key)
|
|
23
|
+
when :password: html << typus_password_field(key)
|
|
24
|
+
when :selector: html << typus_selector_field(key)
|
|
25
|
+
when :text: html << typus_text_field(key)
|
|
26
|
+
when :time: html << typus_time_field(key, options)
|
|
27
|
+
when :tree: html << typus_tree_field(key)
|
|
28
|
+
else
|
|
29
|
+
html << typus_string_field(key)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
html << '</ul>'
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def typus_belongs_to_field(attribute, klass = @resource[:class])
|
|
37
|
+
|
|
38
|
+
##
|
|
39
|
+
# We only can pass parameters to 'new' and 'edit', so this hack makes
|
|
40
|
+
# the work to replace the current action.
|
|
41
|
+
#
|
|
42
|
+
params[:action] = (params[:action] == 'create') ? 'new' : params[:action]
|
|
43
|
+
|
|
44
|
+
back_to = '/' + [ params[:controller], params[:action], params[:id] ].compact.join('/')
|
|
45
|
+
|
|
46
|
+
related = klass.reflect_on_association(attribute.to_sym).class_name.constantize
|
|
47
|
+
related_fk = klass.reflect_on_association(attribute.to_sym).primary_key_name
|
|
48
|
+
|
|
49
|
+
message = [ _("Are you sure you want to leave this page?"),
|
|
50
|
+
_("If you have made any changes to the fields without clicking the Save/Update entry button, your changes will be lost."),
|
|
51
|
+
_("Click OK to continue, or click Cancel to stay on this page.") ]
|
|
52
|
+
|
|
53
|
+
returning(String.new) do |html|
|
|
54
|
+
|
|
55
|
+
if related.respond_to?(:roots)
|
|
56
|
+
html << typus_tree_field(related_fk, related.roots, related_fk)
|
|
57
|
+
else
|
|
58
|
+
html << <<-HTML
|
|
59
|
+
<li><label for="item_#{attribute}">#{_(related_fk.humanize)}
|
|
60
|
+
<small>#{link_to _('Add'), { :controller => "admin/#{related.class_name.tableize}", :action => 'new', :back_to => back_to, :selected => related_fk }, :confirm => message.join("\n\n") if @current_user.can_perform?(related, 'create')}</small>
|
|
61
|
+
</label>
|
|
62
|
+
#{select :item, related_fk, related.find(:all, :order => related.typus_order_by).collect { |p| [p.typus_name, p.id] }, { :include_blank => true }, { :disabled => attribute_disabled?(attribute, klass) } }</li>
|
|
63
|
+
HTML
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def typus_boolean_field(attribute, klass = @resource[:class])
|
|
71
|
+
<<-HTML
|
|
72
|
+
<li><label for="item_#{attribute}">#{klass.human_attribute_name(attribute)}</label>
|
|
73
|
+
#{check_box :item, attribute} #{_('Checked if active')}</li>
|
|
74
|
+
HTML
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def typus_date_field(attribute, options, klass = @resource[:class])
|
|
78
|
+
<<-HTML
|
|
79
|
+
<li><label for="item_#{attribute}">#{klass.human_attribute_name(attribute)}</label>
|
|
80
|
+
#{date_select :item, attribute, options, { :disabled => attribute_disabled?(attribute, klass)} }</li>
|
|
81
|
+
HTML
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def typus_datetime_field(attribute, options, klass = @resource[:class])
|
|
85
|
+
<<-HTML
|
|
86
|
+
<li><label for="item_#{attribute}">#{klass.human_attribute_name(attribute)}</label>
|
|
87
|
+
#{datetime_select :item, attribute, options, {:disabled => attribute_disabled?(attribute, klass)}}</li>
|
|
88
|
+
HTML
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def typus_file_field(attribute, klass = @resource[:class])
|
|
92
|
+
|
|
93
|
+
attribute_display = attribute.split('_file_name').first
|
|
94
|
+
|
|
95
|
+
<<-HTML
|
|
96
|
+
<li><label for="item_#{attribute}">#{_(attribute_display.humanize)}</label>
|
|
97
|
+
#{file_field :item, attribute.split("_file_name").first, :disabled => attribute_disabled?(attribute, klass)}</li>
|
|
98
|
+
HTML
|
|
99
|
+
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def typus_password_field(attribute, klass = @resource[:class])
|
|
103
|
+
<<-HTML
|
|
104
|
+
<li><label for="item_#{attribute}">#{klass.human_attribute_name(attribute)}</label>
|
|
105
|
+
#{password_field :item, attribute, :class => 'text', :disabled => attribute_disabled?(attribute, klass)}</li>
|
|
106
|
+
HTML
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def typus_selector_field(attribute, klass = @resource[:class])
|
|
110
|
+
returning(String.new) do |html|
|
|
111
|
+
options = []
|
|
112
|
+
@resource[:class].send(attribute).each do |option|
|
|
113
|
+
case option.kind_of?(Array)
|
|
114
|
+
when true
|
|
115
|
+
selected = (@item.send(attribute).to_s == option.last.to_s) ? 'selected' : ''
|
|
116
|
+
options << "<option #{selected} value=\"#{option.last}\">#{option.first}</option>"
|
|
117
|
+
else
|
|
118
|
+
selected = (@item.send(attribute).to_s == option.to_s) ? 'selected' : ''
|
|
119
|
+
options << "<option #{selected} value=\"#{option}\">#{option}</option>"
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
html << <<-HTML
|
|
123
|
+
<li><label for="item_#{attribute}">#{klass.human_attribute_name(attribute)}</label>
|
|
124
|
+
<select id="item_#{attribute}" #{attribute_disabled?(attribute) ? 'disabled="disabled"' : ''} name="item[#{attribute}]">
|
|
125
|
+
<option value=""></option>
|
|
126
|
+
#{options.join("\n")}
|
|
127
|
+
</select></li>
|
|
128
|
+
HTML
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def typus_text_field(attribute, klass = @resource[:class])
|
|
133
|
+
<<-HTML
|
|
134
|
+
<li><label for="item_#{attribute}">#{klass.human_attribute_name(attribute)}</label>
|
|
135
|
+
#{text_area :item, attribute, :class => 'text', :rows => klass.typus_options_for(:form_rows), :disabled => attribute_disabled?(attribute, klass)}</li>
|
|
136
|
+
HTML
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def typus_time_field(attribute, options, klass = @resource[:class])
|
|
140
|
+
<<-HTML
|
|
141
|
+
<li><label for="item_#{attribute}">#{klass.human_attribute_name(attribute)}</label>
|
|
142
|
+
#{time_select :item, attribute, options, {:disabled => attribute_disabled?(attribute, klass)}}</li>
|
|
143
|
+
HTML
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def typus_tree_field(attribute, items = @resource[:class].roots, attribute_virtual = 'parent_id', klass = @resource[:class])
|
|
147
|
+
<<-HTML
|
|
148
|
+
<li><label for="item_#{attribute}">#{klass.human_attribute_name(attribute)}</label>
|
|
149
|
+
<select id="item_#{attribute}" #{attribute_disabled?(attribute) ? 'disabled="disabled"' : ''} name="item[#{attribute}]">
|
|
150
|
+
<option value=""></option>
|
|
151
|
+
#{expand_tree_into_select_field(items, attribute_virtual)}
|
|
152
|
+
</select></li>
|
|
153
|
+
HTML
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def typus_string_field(attribute, klass = @resource[:class])
|
|
157
|
+
|
|
158
|
+
# Read only fields.
|
|
159
|
+
if klass.typus_field_options_for(:read_only).include?(attribute)
|
|
160
|
+
value = 'read_only' if %w( edit ).include?(params[:action])
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# Auto generated fields.
|
|
164
|
+
if klass.typus_field_options_for(:auto_generated).include?(attribute)
|
|
165
|
+
value = 'auto_generated' if %w( new edit ).include?(params[:action])
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
comment = %w( read_only auto_generated ).include?(value) ? "<small>#{value} field</small>".humanize : ''
|
|
169
|
+
|
|
170
|
+
attribute_humanized = klass.human_attribute_name(attribute)
|
|
171
|
+
attribute_humanized += " (#{attribute})" if attribute.include?('_id')
|
|
172
|
+
|
|
173
|
+
<<-HTML
|
|
174
|
+
<li><label for="item_#{attribute}">#{attribute_humanized}#{comment}</label>
|
|
175
|
+
#{text_field :item, attribute, :class => 'text', :disabled => attribute_disabled?(attribute, klass) }</li>
|
|
176
|
+
HTML
|
|
177
|
+
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def typus_relationships
|
|
181
|
+
|
|
182
|
+
@back_to = '/' + [ params[:controller], params[:action], params[:id] ].compact.join('/')
|
|
183
|
+
|
|
184
|
+
returning(String.new) do |html|
|
|
185
|
+
@resource[:class].typus_defaults_for(:relationships).each do |relationship|
|
|
186
|
+
case @resource[:class].reflect_on_association(relationship.to_sym).macro
|
|
187
|
+
when :has_and_belongs_to_many
|
|
188
|
+
html << typus_form_has_and_belongs_to_many(relationship)
|
|
189
|
+
when :has_many
|
|
190
|
+
html << typus_form_has_many(relationship)
|
|
191
|
+
when :has_one
|
|
192
|
+
html << typus_form_has_one(relationship)
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def typus_form_has_many(field)
|
|
200
|
+
returning(String.new) do |html|
|
|
201
|
+
|
|
202
|
+
model_to_relate = @resource[:class].reflect_on_association(field.to_sym).class_name.constantize
|
|
203
|
+
model_to_relate_as_resource = model_to_relate.name.tableize
|
|
204
|
+
|
|
205
|
+
reflection = @resource[:class].reflect_on_association(field.to_sym)
|
|
206
|
+
association = reflection.macro
|
|
207
|
+
foreign_key = reflection.through_reflection ? reflection.primary_key_name.pluralize : reflection.primary_key_name
|
|
208
|
+
|
|
209
|
+
link_options = { :controller => "admin/#{field}",
|
|
210
|
+
:action => 'new',
|
|
211
|
+
:back_to => @back_to,
|
|
212
|
+
:resource => @resource[:self].singularize,
|
|
213
|
+
:resource_id => @item.id,
|
|
214
|
+
foreign_key => @item.id }
|
|
215
|
+
|
|
216
|
+
html << <<-HTML
|
|
217
|
+
<a name="#{field}"></a>
|
|
218
|
+
<div class="box_relationships">
|
|
219
|
+
<h2>
|
|
220
|
+
#{link_to model_to_relate.human_name.pluralize, :controller => "admin/#{model_to_relate_as_resource}"}
|
|
221
|
+
<small>#{link_to _('Add new'), link_options if @current_user.can_perform?(model_to_relate, 'create')}</small>
|
|
222
|
+
</h2>
|
|
223
|
+
HTML
|
|
224
|
+
items = @resource[:class].find(params[:id]).send(field).find(:all, :order => model_to_relate.typus_order_by)
|
|
225
|
+
unless items.empty?
|
|
226
|
+
options = { :back_to => @back_to, :resource => @resource[:self], :resource_id => @item.id }
|
|
227
|
+
html << build_list(model_to_relate,
|
|
228
|
+
model_to_relate.typus_fields_for(:relationship),
|
|
229
|
+
items,
|
|
230
|
+
model_to_relate_as_resource,
|
|
231
|
+
options,
|
|
232
|
+
association)
|
|
233
|
+
else
|
|
234
|
+
html << <<-HTML
|
|
235
|
+
<div id="flash" class="notice"><p>#{_("There are no {{records}}.", :records => model_to_relate.human_name.pluralize.downcase)}</p></div>
|
|
236
|
+
HTML
|
|
237
|
+
end
|
|
238
|
+
html << <<-HTML
|
|
239
|
+
</div>
|
|
240
|
+
HTML
|
|
241
|
+
end
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
def typus_form_has_and_belongs_to_many(field)
|
|
245
|
+
returning(String.new) do |html|
|
|
246
|
+
|
|
247
|
+
model_to_relate = @resource[:class].reflect_on_association(field.to_sym).class_name.constantize
|
|
248
|
+
model_to_relate_as_resource = model_to_relate.name.tableize
|
|
249
|
+
|
|
250
|
+
reflection = @resource[:class].reflect_on_association(field.to_sym)
|
|
251
|
+
association = reflection.macro
|
|
252
|
+
|
|
253
|
+
html << <<-HTML
|
|
254
|
+
<a name="#{field}"></a>
|
|
255
|
+
<div class="box_relationships">
|
|
256
|
+
<h2>
|
|
257
|
+
#{link_to model_to_relate.human_name.pluralize, :controller => "admin/#{model_to_relate_as_resource}"}
|
|
258
|
+
<small>#{link_to _('Add new'), :controller => field, :action => 'new', :back_to => @back_to, :resource => @resource[:self], :resource_id => @item.id if @current_user.can_perform?(model_to_relate, 'create')}</small>
|
|
259
|
+
</h2>
|
|
260
|
+
HTML
|
|
261
|
+
items_to_relate = (model_to_relate.find(:all) - @item.send(field))
|
|
262
|
+
unless items_to_relate.empty?
|
|
263
|
+
html << <<-HTML
|
|
264
|
+
#{form_tag :action => 'relate', :id => @item.id}
|
|
265
|
+
#{hidden_field :related, :model, :value => model_to_relate}
|
|
266
|
+
<p>#{ select :related, :id, items_to_relate.collect { |f| [f.typus_name, f.id] }.sort_by { |e| e.first } } #{submit_tag _('Add'), :class => 'button'}</p>
|
|
267
|
+
</form>
|
|
268
|
+
HTML
|
|
269
|
+
end
|
|
270
|
+
items = @resource[:class].find(params[:id]).send(field)
|
|
271
|
+
unless items.empty?
|
|
272
|
+
html << build_list(model_to_relate,
|
|
273
|
+
model_to_relate.typus_fields_for(:relationship),
|
|
274
|
+
items,
|
|
275
|
+
model_to_relate_as_resource,
|
|
276
|
+
{},
|
|
277
|
+
association)
|
|
278
|
+
else
|
|
279
|
+
html << <<-HTML
|
|
280
|
+
<div id="flash" class="notice"><p>#{_("There are no {{records}}.", :records => model_to_relate.human_name.pluralize.downcase)}</p></div>
|
|
281
|
+
HTML
|
|
282
|
+
end
|
|
283
|
+
html << <<-HTML
|
|
284
|
+
</div>
|
|
285
|
+
HTML
|
|
286
|
+
end
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
def typus_form_has_one(field)
|
|
290
|
+
returning(String.new) do |html|
|
|
291
|
+
|
|
292
|
+
model_to_relate = @resource[:class].reflect_on_association(field.to_sym).class_name.constantize
|
|
293
|
+
model_to_relate_as_resource = model_to_relate.name.tableize
|
|
294
|
+
|
|
295
|
+
reflection = @resource[:class].reflect_on_association(field.to_sym)
|
|
296
|
+
association = reflection.macro
|
|
297
|
+
|
|
298
|
+
html << <<-HTML
|
|
299
|
+
<a name="#{field}"></a>
|
|
300
|
+
<div class="box_relationships">
|
|
301
|
+
<h2>
|
|
302
|
+
#{link_to model_to_relate.human_name, :controller => "admin/#{model_to_relate_as_resource}"}
|
|
303
|
+
</h2>
|
|
304
|
+
HTML
|
|
305
|
+
items = Array.new
|
|
306
|
+
items << @resource[:class].find(params[:id]).send(field) unless @resource[:class].find(params[:id]).send(field).nil?
|
|
307
|
+
unless items.empty?
|
|
308
|
+
options = { :back_to => @back_to, :resource => @resource[:self], :resource_id => @item.id }
|
|
309
|
+
html << build_list(model_to_relate,
|
|
310
|
+
model_to_relate.typus_fields_for(:relationship),
|
|
311
|
+
items,
|
|
312
|
+
model_to_relate_as_resource,
|
|
313
|
+
options,
|
|
314
|
+
association)
|
|
315
|
+
else
|
|
316
|
+
html << <<-HTML
|
|
317
|
+
<div id="flash" class="notice"><p>#{_("There is no {{records}}.", :records => model_to_relate.human_name.downcase)}</p></div>
|
|
318
|
+
HTML
|
|
319
|
+
end
|
|
320
|
+
html << <<-HTML
|
|
321
|
+
</div>
|
|
322
|
+
HTML
|
|
323
|
+
end
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
def typus_template_field(attribute, template, options = {})
|
|
327
|
+
folder = Typus::Configuration.options[:templates_folder]
|
|
328
|
+
template_name = File.join(folder, template)
|
|
329
|
+
|
|
330
|
+
output = render(:partial => template_name, :locals => { :resource => @resource, :attribute => attribute, :options => options } )
|
|
331
|
+
output || "#{attribute}: Can not find the template '#{template}'"
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
def attribute_disabled?(attribute, klass = @resource[:class])
|
|
335
|
+
accessible = klass.accessible_attributes
|
|
336
|
+
return accessible.nil? ? false : !accessible.include?(attribute)
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
##
|
|
340
|
+
# Tree builder when model +acts_as_tree+
|
|
341
|
+
#
|
|
342
|
+
def expand_tree_into_select_field(items, attribute)
|
|
343
|
+
returning(String.new) do |html|
|
|
344
|
+
items.each do |item|
|
|
345
|
+
html << %{<option #{"selected" if @item.send(attribute) == item.id} value="#{item.id}">#{" " * item.ancestors.size * 8} \_ #{item.typus_name}</option>\n}
|
|
346
|
+
html << expand_tree_into_select_field(item.children, attribute) unless item.children.empty?
|
|
347
|
+
end
|
|
348
|
+
end
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
end
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
module Admin::MasterHelper
|
|
2
|
+
|
|
3
|
+
include TypusHelper
|
|
4
|
+
|
|
5
|
+
include Admin::SidebarHelper
|
|
6
|
+
include Admin::FormHelper
|
|
7
|
+
include Admin::TableHelper
|
|
8
|
+
|
|
9
|
+
def display_link_to_previous(klass = @resource[:class], _params = params)
|
|
10
|
+
|
|
11
|
+
options = {}
|
|
12
|
+
options[:resource_from] = klass.human_name
|
|
13
|
+
options[:resource_to] = _params[:resource].classify.humanize if _params[:resource]
|
|
14
|
+
|
|
15
|
+
editing = %w( edit update ).include?(_params[:action])
|
|
16
|
+
|
|
17
|
+
message = case
|
|
18
|
+
when _params[:resource] && editing
|
|
19
|
+
_("You're updating a {{resource_from}} for {{resource_to}}.",
|
|
20
|
+
:resource_from => options[:resource_from],
|
|
21
|
+
:resource_to => options[:resource_to])
|
|
22
|
+
when editing
|
|
23
|
+
_("You're updating a {{resource_from}}.",
|
|
24
|
+
:resource_from => options[:resource_from])
|
|
25
|
+
when _params[:resource]
|
|
26
|
+
_("You're adding a new {{resource_from}} to {{resource_to}}.",
|
|
27
|
+
:resource_from => options[:resource_from],
|
|
28
|
+
:resource_to => options[:resource_to])
|
|
29
|
+
else
|
|
30
|
+
_("You're adding a new {{resource_from}}.",
|
|
31
|
+
:resource_from => options[:resource_from] )
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
returning(String.new) do |html|
|
|
35
|
+
html << <<-HTML
|
|
36
|
+
<div id="flash" class="notice">
|
|
37
|
+
<p>#{message} #{link_to _("Do you want to cancel it?"), _params[:back_to]}</p>
|
|
38
|
+
</div>
|
|
39
|
+
HTML
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def remove_filter_link(filter = request.env['QUERY_STRING'])
|
|
45
|
+
return unless filter && !filter.blank?
|
|
46
|
+
<<-HTML
|
|
47
|
+
<small>#{link_to _('Remove filter')}</small>
|
|
48
|
+
HTML
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
##
|
|
52
|
+
# If there's a partial with a "microformat" of the data we want to
|
|
53
|
+
# display, this will be used, otherwise we use a default table which
|
|
54
|
+
# it's build from the options defined on the yaml configuration file.
|
|
55
|
+
#
|
|
56
|
+
def build_list(model, fields, items, resource = @resource[:self], link_options = {}, association = nil)
|
|
57
|
+
|
|
58
|
+
template = "app/views/admin/#{resource}/_#{resource.singularize}.html.erb"
|
|
59
|
+
|
|
60
|
+
if File.exists?(template)
|
|
61
|
+
render :partial => template.gsub('/_', '/'), :collection => items, :as => :item
|
|
62
|
+
else
|
|
63
|
+
build_typus_table(model, fields, items, link_options, association)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
##
|
|
69
|
+
# Simple and clean pagination links
|
|
70
|
+
#
|
|
71
|
+
def build_pagination(pager, options = {})
|
|
72
|
+
|
|
73
|
+
options[:link_to_current_page] ||= true
|
|
74
|
+
options[:always_show_anchors] ||= true
|
|
75
|
+
|
|
76
|
+
# Calculate the window start and end pages
|
|
77
|
+
options[:padding] ||= 2
|
|
78
|
+
options[:padding] = options[:padding] < 0 ? 0 : options[:padding]
|
|
79
|
+
|
|
80
|
+
page = params[:page].blank? ? 1 : params[:page].to_i
|
|
81
|
+
current_page = pager.page(page)
|
|
82
|
+
|
|
83
|
+
first = pager.first.number <= (current_page.number - options[:padding]) && pager.last.number >= (current_page.number - options[:padding]) ? current_page.number - options[:padding] : 1
|
|
84
|
+
last = pager.first.number <= (current_page.number + options[:padding]) && pager.last.number >= (current_page.number + options[:padding]) ? current_page.number + options[:padding] : pager.last.number
|
|
85
|
+
|
|
86
|
+
returning(String.new) do |html|
|
|
87
|
+
# Print start page if anchors are enabled
|
|
88
|
+
html << yield(1) if options[:always_show_anchors] and not first == 1
|
|
89
|
+
# Print window pages
|
|
90
|
+
first.upto(last) do |page|
|
|
91
|
+
(current_page.number == page && !options[:link_to_current_page]) ? html << page.to_s : html << (yield(page)).to_s
|
|
92
|
+
end
|
|
93
|
+
# Print end page if anchors are enabled
|
|
94
|
+
html << yield(pager.last.number).to_s if options[:always_show_anchors] and not last == pager.last.number
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module Admin
|
|
2
|
+
|
|
3
|
+
module PublicHelper
|
|
4
|
+
|
|
5
|
+
def quick_edit(*args)
|
|
6
|
+
|
|
7
|
+
options = args.extract_options!
|
|
8
|
+
options[:color] ||= '#000'
|
|
9
|
+
options[:link] ||= admin_quick_edit_path
|
|
10
|
+
|
|
11
|
+
query = options.dup
|
|
12
|
+
[ :color, :link ].each { |o| query.delete(o) }
|
|
13
|
+
|
|
14
|
+
<<-HTML
|
|
15
|
+
<script type="text/javascript">
|
|
16
|
+
document.write('<script type="text/javascript" src="#{options[:link]}?#{query.to_query}" />');
|
|
17
|
+
</script>
|
|
18
|
+
HTML
|
|
19
|
+
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
end
|