From 34c5d12ebf95baea91bded7d4fe17bf9b17f6179 Mon Sep 17 00:00:00 2001 From: did Date: Thu, 19 May 2011 18:00:45 +0200 Subject: [PATCH] refactoring the rendering of content fields in a model --- .../custom_fields/_custom_form.html.haml | 67 +------------------ .../custom_fields/types/_boolean.html.haml | 2 + .../custom_fields/types/_category.html.haml | 4 ++ .../admin/custom_fields/types/_date.html.haml | 1 + .../admin/custom_fields/types/_file.html.haml | 10 +++ .../custom_fields/types/_has_many.html.haml | 31 +++++++++ .../custom_fields/types/_has_one.html.haml | 2 + .../custom_fields/types/_string.html.haml | 1 + .../admin/custom_fields/types/_text.html.haml | 1 + 9 files changed, 53 insertions(+), 66 deletions(-) create mode 100644 app/views/admin/custom_fields/types/_boolean.html.haml create mode 100644 app/views/admin/custom_fields/types/_category.html.haml create mode 100644 app/views/admin/custom_fields/types/_date.html.haml create mode 100644 app/views/admin/custom_fields/types/_file.html.haml create mode 100644 app/views/admin/custom_fields/types/_has_many.html.haml create mode 100644 app/views/admin/custom_fields/types/_has_one.html.haml create mode 100644 app/views/admin/custom_fields/types/_string.html.haml create mode 100644 app/views/admin/custom_fields/types/_text.html.haml diff --git a/app/views/admin/custom_fields/_custom_form.html.haml b/app/views/admin/custom_fields/_custom_form.html.haml index 97e710b1..92ed6b7c 100644 --- a/app/views/admin/custom_fields/_custom_form.html.haml +++ b/app/views/admin/custom_fields/_custom_form.html.haml @@ -4,71 +4,6 @@ - form.object.custom_fields.each do |field| - required = highlighted_field_name == field._name || field.required - - if field.string? - = form.input field._alias.to_sym, :label => field.label, :hint => field.hint, :required => required - - - elsif field.text? - = form.input field._alias.to_sym, :label => field.label, :hint => field.hint, :as => :text, :required => required, :input_html => { :class => field.text_formatting } - - - elsif field.category? - = form.custom_input field._alias.to_sym, :label => field.label, :hint => field.hint, :css => 'toggle' do - = form.select field._name.to_sym, field.ordered_category_items.collect { |item| [item.name, item.id] } - %button.button.light.edit-categories-link{ :type => 'button', :'data-url' => edit_admin_custom_field_path(parent.class.model_name.underscore, parent.slug, field) } - %span!= t('.edit_categories') - - - elsif field.boolean? - = form.custom_input field._alias.to_sym, :label => field.label, :hint => field.hint, :css => 'toggle' do - = form.check_box field._alias.to_sym - - - elsif field.date? - = form.input field._alias.to_sym, :label => field.label, :hint => field.hint, :input_html => { :class => 'date' } - - - elsif field.file? - = form.custom_input field._alias.to_sym, :label => field.label, :hint => field.hint, :css => 'file' do - = form.file_field field._name.to_sym - - if form.object.send(:"#{field._name}?") - %p.remove - %strong - = link_to File.basename(form.object.send(field._name).url), form.object.send(field._name).url - %span -  /  - != t('.delete_file') - = form.check_box :"remove_#{field._name}" - - - elsif field.has_one? - = form.input field._alias.to_sym, :label => field.label, :hint => field.hint, :input_html => { :class => 'has_one' }, :as => :select, :collection => options_for_has_one(field), :selected => form.object.send(field._alias.to_sym).try(:_id) - - - elsif field.has_many? - = form.custom_input field._alias.to_sym, :label => field.label, :hint => field.hint, :css => 'has-many' do - - .has-many-selector - - %p{ :style => form.object.send(field._alias.to_sym).empty? ? '' : 'display: none' } - = t('.empty') - - %ul - - %script{ :type => 'text/x-mustache-template', :name => 'template', :'data-base-input-name' => "content[#{field._alias.to_sym}]" } - %li{ :class => "item {{behaviour_flag}}" } - %span.handle - = image_tag 'admin/form/icons/drag.png' - - {{^if_template}} - %input{ :name => '{{base_name}}[]', :value => '{{{id}}}', :type => 'hidden', :'data-field' => 'id' } - {{/if_template}} - - %strong {{label}} - - {{#if_template}} - = select_tag 'label', '' - {{/if_template}} - - %span.actions - = link_to image_tag('admin/form/icons/trash.png'), '#', :class => 'remove' - %button{ :class => 'button light mini add', :type => 'button' } - %span!= t('admin.buttons.new_item') - - %script{ :type => 'text/javascript', :name => 'data' } - != collection_to_js(options_for_has_many(field), :taken_ids => form.object.send(field._alias.to_sym).ids) + = render "/admin/custom_fields/types/#{field.kind}", :form => form, :parent => parent, :field => field, :required => required = render '/admin/custom_fields/category_tmpl' \ No newline at end of file diff --git a/app/views/admin/custom_fields/types/_boolean.html.haml b/app/views/admin/custom_fields/types/_boolean.html.haml new file mode 100644 index 00000000..2ad89277 --- /dev/null +++ b/app/views/admin/custom_fields/types/_boolean.html.haml @@ -0,0 +1,2 @@ += form.custom_input field._alias.to_sym, :label => field.label, :hint => field.hint, :css => 'toggle' do + = form.check_box field._alias.to_sym diff --git a/app/views/admin/custom_fields/types/_category.html.haml b/app/views/admin/custom_fields/types/_category.html.haml new file mode 100644 index 00000000..e0ec624d --- /dev/null +++ b/app/views/admin/custom_fields/types/_category.html.haml @@ -0,0 +1,4 @@ += form.custom_input field._alias.to_sym, :label => field.label, :hint => field.hint, :css => 'toggle' do + = form.select field._name.to_sym, field.ordered_category_items.collect { |item| [item.name, item.id] } + %button.button.light.edit-categories-link{ :type => 'button', :'data-url' => edit_admin_custom_field_path(parent.class.model_name.underscore, parent.slug, field) } + %span!= t('.edit_categories') diff --git a/app/views/admin/custom_fields/types/_date.html.haml b/app/views/admin/custom_fields/types/_date.html.haml new file mode 100644 index 00000000..445f2e63 --- /dev/null +++ b/app/views/admin/custom_fields/types/_date.html.haml @@ -0,0 +1 @@ += form.input field._alias.to_sym, :label => field.label, :hint => field.hint, :input_html => { :class => 'date' } \ No newline at end of file diff --git a/app/views/admin/custom_fields/types/_file.html.haml b/app/views/admin/custom_fields/types/_file.html.haml new file mode 100644 index 00000000..955291f6 --- /dev/null +++ b/app/views/admin/custom_fields/types/_file.html.haml @@ -0,0 +1,10 @@ += form.custom_input field._alias.to_sym, :label => field.label, :hint => field.hint, :css => 'file' do + = form.file_field field._name.to_sym + - if form.object.send(:"#{field._name}?") + %p.remove + %strong + = link_to File.basename(form.object.send(field._name).url), form.object.send(field._name).url + %span +  /  + != t('.delete_file') + = form.check_box :"remove_#{field._name}" diff --git a/app/views/admin/custom_fields/types/_has_many.html.haml b/app/views/admin/custom_fields/types/_has_many.html.haml new file mode 100644 index 00000000..a6a40f7d --- /dev/null +++ b/app/views/admin/custom_fields/types/_has_many.html.haml @@ -0,0 +1,31 @@ += form.custom_input field._alias.to_sym, :label => field.label, :hint => field.hint, :css => 'has-many' do + + .has-many-selector + + %p{ :style => form.object.send(field._alias.to_sym).empty? ? '' : 'display: none' } + = t('.empty') + + %ul + + %script{ :type => 'text/x-mustache-template', :name => 'template', :'data-base-input-name' => "content[#{field._alias.to_sym}]" } + %li{ :class => "item {{behaviour_flag}}" } + %span.handle + = image_tag 'admin/form/icons/drag.png' + + {{^if_template}} + %input{ :name => '{{base_name}}[]', :value => '{{{id}}}', :type => 'hidden', :'data-field' => 'id' } + {{/if_template}} + + %strong {{label}} + + {{#if_template}} + = select_tag 'label', '' + {{/if_template}} + + %span.actions + = link_to image_tag('admin/form/icons/trash.png'), '#', :class => 'remove' + %button{ :class => 'button light mini add', :type => 'button' } + %span!= t('admin.buttons.new_item') + + %script{ :type => 'text/javascript', :name => 'data' } + != collection_to_js(options_for_has_many(field), :taken_ids => form.object.send(field._alias.to_sym).ids) diff --git a/app/views/admin/custom_fields/types/_has_one.html.haml b/app/views/admin/custom_fields/types/_has_one.html.haml new file mode 100644 index 00000000..67c3aaec --- /dev/null +++ b/app/views/admin/custom_fields/types/_has_one.html.haml @@ -0,0 +1,2 @@ += form.input field._alias.to_sym, :label => field.label, :hint => field.hint, :input_html => { :class => 'has_one' }, :as => :select, :collection => options_for_has_one(field), :selected => form.object.send(field._alias.to_sym).try(:_id) + diff --git a/app/views/admin/custom_fields/types/_string.html.haml b/app/views/admin/custom_fields/types/_string.html.haml new file mode 100644 index 00000000..d35db032 --- /dev/null +++ b/app/views/admin/custom_fields/types/_string.html.haml @@ -0,0 +1 @@ += form.input field._alias.to_sym, :label => field.label, :hint => field.hint, :required => required \ No newline at end of file diff --git a/app/views/admin/custom_fields/types/_text.html.haml b/app/views/admin/custom_fields/types/_text.html.haml new file mode 100644 index 00000000..b4f6192d --- /dev/null +++ b/app/views/admin/custom_fields/types/_text.html.haml @@ -0,0 +1 @@ += form.input field._alias.to_sym, :label => field.label, :hint => field.hint, :as => :text, :required => required, :input_html => { :class => field.text_formatting } \ No newline at end of file