Module: Htmlful::DynamicFields
- Defined in:
- lib/htmlful/dynamic_fields.rb
Instance Method Summary collapse
- #_dynamic_fields(form, resource, relationship_name, block1, block2) ⇒ Object
- #dynamic_fields(form, resource, relationship_name, *attributes) ⇒ Object
- #form_inputs(form, *attributes) ⇒ Object
- #multi_select_input(form, association, options = {}) ⇒ Object
-
#show_attribute(form, resource, attribute) ⇒ Object
inside of a form.
-
#show_attribute_outside_form(resource, attribute, options = nil, &block) ⇒ Object
TODO: use concat and usage will be nicer.
- #show_dynamic_fields(form, resource, relationship_name, *attributes) ⇒ Object
- #show_subcollection(form, resource, association) ⇒ Object
Instance Method Details
#_dynamic_fields(form, resource, relationship_name, block1, block2) ⇒ Object
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 |
# File 'lib/htmlful/dynamic_fields.rb', line 3 def _dynamic_fields(form, resource, relationship_name, block1, block2) relationship_i18n_name = resource.class.human_attribute_name(relationship_name).to_s form.inputs :title => relationship_name do unless resource.send(relationship_name).empty? form.semantic_fields_for(relationship_name) do |sub_form| sub_form.inputs do block1.call(sub_form) concat sub_form.input(:_delete, :as => :hidden, :wrapper_html => {:class => 'remove'}, :input_html => {:class => "checkbox_remove"}) concat content_tag(:li, link_to(t(:remove_nested_element, :resource_name => relationship_i18n_name), '#', :class => "remove_fieldset")) end end end concat content_tag(:div, :class => "new_nested_element") { concat content_tag(:div, :class => "nested_inputs") { form.inputs do form.semantic_fields_for relationship_name, resource.send(relationship_name).build, :child_index => "NEW_RECORD" do |sub_form| block2.call(sub_form) end end } concat link_to(t(:remove_nested_element, :resource_name => relationship_i18n_name), '#', :class => "remove_element") concat link_to(t(:create_nested_element, :resource_name => relationship_i18n_name), "#", :class => "create_element") } end end |
#dynamic_fields(form, resource, relationship_name, *attributes) ⇒ Object
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 |
# File 'lib/htmlful/dynamic_fields.rb', line 29 def dynamic_fields(form, resource, relationship_name, *attributes) block1 = lambda do |sub_form| sub_object = sub_form.object attributes.each do |attribute| if is_date(sub_object, attribute) concat sub_form.input(attribute, :as => :string, :wrapper_html => {:class => 'datepick'}) elsif is_datetime(sub_object, attribute) concat sub_form.input(attribute, :include_blank => false) elsif is_document(sub_object, attribute) if is_document_empty?(sub_object, attribute) concat content_tag(:li, content_tag(:p, t(:no_document))) else if is_image(sub_object, attribute) concat image_tag(sub_form.object.send(attribute).url(:thumb)) else concat content_tag(:li, content_tag(:p, link_to(sub_object.send("#{attribute}_file_name"), sub_object.send(attribute).url))) end end else concat sub_form.input(attribute) end end end block2 = lambda do |sub_form| sub_object = sub_form.object attributes.each do |attribute| if is_date(sub_object, attribute) concat sub_form.input(attribute, :as => :string, :wrapper_html => {:class => 'datepick ignore'}) elsif is_datetime(sub_object, attribute) concat sub_form.input(attribute, :include_blank => false) else concat sub_form.input(attribute) # takes care of everything else end end end _dynamic_fields(form, resource, relationship_name, block1, block2) end |
#form_inputs(form, *attributes) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/htmlful/dynamic_fields.rb', line 145 def form_inputs(form, *attributes) = attributes. resource = form.object returning("") do |html| attributes.each do |attribute| html << form.input(attribute) if is_document(resource, attribute) unless is_document_empty?(resource, attribute) html << "<li>" if is_image(resource, attribute) image_style = (.nil? || [:image_style].nil?)? :thumb : [:image_style] html << image_tag(form.object.send(attribute).url(image_style)) else html << link_to(sub_object.send("#{attribute}_file_name"), resource.send(attribute).url) end html << content_tag(:br) html << content_tag(:a, t(:delete_resource, :resource => resource.class.human_attribute_name(attribute)), :href => "#", :class => "delete_document #{resource.class.name.underscore} #{attribute}") html << "</li>" end end end end end |
#multi_select_input(form, association, options = {}) ⇒ Object
141 142 143 |
# File 'lib/htmlful/dynamic_fields.rb', line 141 def multi_select_input(form, association, ={}) form.input(association, ) + content_tag(:li, content_tag(:div, link_to(t(:clear_selected), "#", :class => "clear_multi_select"), :class => "clear_multi_select")) end |
#show_attribute(form, resource, attribute) ⇒ Object
inside of a form
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/htmlful/dynamic_fields.rb', line 104 def show_attribute(form, resource, attribute) if is_date(resource, attribute) form.input(attribute, :as => :string, :wrapper_html => {:class => 'datepick'}, :input_html => {:disabled => true}) elsif is_document(resource, attribute) content_tag(:fieldset) do content_tag(:legend) do content_tag(:label, I18n.t("formtastic.labels.#{resource.class.name.underscore}.#{attribute}")) end + if is_document_empty?(resource, attribute) t(:no_document) else if is_image(resource, attribute) image_tag(resource.send(attribute).url(:thumb)) else link_to(resource.send("#{attribute}_file_name"), resource.send(attribute).url) end end end else form.input(attribute, :input_html => {:disabled => true}) end end |
#show_attribute_outside_form(resource, attribute, options = nil, &block) ⇒ Object
TODO: use concat and usage will be nicer
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/htmlful/dynamic_fields.rb', line 84 def show_attribute_outside_form(resource, attribute, =nil, &block) if is_date(resource, attribute) resource.send(attribute) # TODO: add the controversial abbr method here, or just use title elsif is_document(resource, attribute) if is_document_empty?(resource, attribute) t(:no_document) else if is_image(resource, attribute) image_style = (.nil? || [:image_style].nil?)? :thumb : [:image_style] image_tag(resource.send(attribute).url(image_style)) else link_to(resource.send("#{attribute}_file_name"), resource.send(attribute).url) end end else yield end end |
#show_dynamic_fields(form, resource, relationship_name, *attributes) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/htmlful/dynamic_fields.rb', line 67 def show_dynamic_fields(form, resource, relationship_name, *attributes) form.inputs :title => relationship_name do if resource.send(relationship_name).empty? concat t(:no_resource_name_plural, :resource_name_plural => resource.class.human_attribute_name(relationship_name, :count => 2).mb_chars.downcase) else form.semantic_fields_for(relationship_name) do |sub_form| sub_form.inputs do attributes.each do |attribute| concat show_attribute(sub_form, sub_form.object, attribute) end end end end end end |
#show_subcollection(form, resource, association) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/htmlful/dynamic_fields.rb', line 127 def show_subcollection(form, resource, association) collection = resource.send(association) resource_name_plural = localized_attribute_string(resource, association.to_sym)#resource.class.reflect_on_association(association.to_sym).klass.human_name(:count => 2) raise ("Translation missing #{params[:locale]}, #{resource.class.human_name}, #{association}") if resource_name_plural.nil? content_tag(:label, resource_name_plural) + if collection.empty? content_tag(:p, I18n.t(:no_resource_name_plural, :resource_name_plural => resource_name_plural.mb_chars.downcase)) else content_tag(:ul, collection.inject("") { |html, sub_resource| html + content_tag(:li, link_to(sub_resource.send(form.send(:detect_label_method, [sub_resource])), sub_resource)) }, :class => "sub-collection") end end |