Guest User

Untitled

a guest
Feb 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.61 KB | None | 0 0
  1. module AdminFormHelper
  2.  
  3. ##
  4. # All helpers related to form.
  5. #
  6.  
  7. def build_form(fields = @item_fields)
  8. returning(String.new) do |html|
  9. html << "#{error_messages_for :item, :header_tag => "h3"}"
  10. html << "<ul>"
  11. fields.each do |field|
  12. case field.last
  13. when "boolean": html << typus_boolean_field(field.first, field.last)
  14. when "datetime": html << typus_datetime_field(field.first, field.last)
  15. when "date": html << typus_date_field(field.first, field.last)
  16. when "text": html << typus_text_field(field.first, field.last)
  17. when "file": html << typus_file_field(field.first, field.last)
  18. when "password": html << typus_password_field(field.first, field.last)
  19. when "selector": html << typus_selector_field(field.first, field.last)
  20. when "collection": html << typus_collection_field(field.first, field.last)
  21. when "tree": html << typus_tree_field(field.first, field.last)
  22. else
  23. html << typus_string_field(field.first, field.last)
  24. end
  25. end
  26. html << "</ul>"
  27. end
  28. end
  29.  
  30. def typus_tree_field(attribute, value)
  31. returning(String.new) do |html|
  32. html << <<-HTML
  33. <li><label for=\"item_#{attribute}\">#{attribute.titleize.capitalize}</label>
  34. <select id="item_#{attribute}" name="item[#{attribute}]" <%= attribute_disabled?(attribute) ? 'disabled="disabled"' : '' %>>>
  35. <option value=""></option>
  36. #{expand_tree_into_select_field(@item.class.top)}
  37. </select></li>
  38. HTML
  39. end
  40. end
  41.  
  42. def typus_datetime_field(attribute, value)
  43. returning(String.new) do |html|
  44. html << <<-HTML
  45. <li><label for="item_#{attribute}">#{attribute.titleize.capitalize}</label>
  46. #{datetime_select :item, attribute, { :minute_step => Typus::Configuration.options[:minute_step] }, {:disabled => attribute_disabled?(attribute)}}</li>
  47. HTML
  48. end
  49. end
  50.  
  51. def typus_date_field(attribute, value)
  52. returning(String.new) do |html|
  53. html << <<-HTML
  54. <li><label for="item_#{attribute}">#{attribute.titleize.capitalize}</label>
  55. #{date_select :item, attribute, { :minute_step => Typus::Configuration.options[:minute_step] }, {:disabled => attribute_disabled?(attribute)}}</li>
  56. HTML
  57. end
  58. end
  59.  
  60. def typus_text_field(attribute, value)
  61. returning(String.new) do |html|
  62. html << <<-HTML
  63. <li><label for="item_#{attribute}">#{attribute.titleize.capitalize}</label>
  64. #{text_area :item, attribute, :class => 'text', :rows => Typus::Configuration.options[:form_rows], :disabled => attribute_disabled?(attribute)}</li>
  65. HTML
  66. end
  67. end
  68.  
  69. def typus_selector_field(attribute, value)
  70. returning(String.new) do |html|
  71. options = ""
  72. @model.send(attribute).each do |option|
  73. case option.kind_of?(Array)
  74. when true
  75. options << <<-HTML
  76. <option #{'selected' if @item.send(attribute).to_s == option.last.to_s} value="#{option.last}">#{option.first}</option>
  77. HTML
  78. else
  79. options << <<-HTML
  80. <option #{'selected' if @item.send(attribute).to_s == option.to_s} value="#{option}">#{option}</option>
  81. HTML
  82. end
  83. end
  84. html << <<-HTML
  85. <li><label for=\"item_#{attribute}\">#{attribute.titleize.capitalize}</label>
  86. <select id="item_#{attribute}" name="item[#{attribute}]" <%= attribute_disabled?(attribute) ? 'disabled="disabled"' : '' %>>
  87. <option value="">Select an option</option>
  88. #{options}
  89. </select></li>
  90. HTML
  91. end
  92. end
  93.  
  94. def typus_collection_field(attribute, value)
  95.  
  96. back_to = "/" + ([] << params[:controller] << params[:id]<< params[:action]).compact.join('/')
  97. related = @model.reflect_on_association(attribute.split("_id").first.to_sym).class_name.constantize
  98.  
  99.  
  100. returning(String.new) do |html|
  101. html << <<-HTML
  102. <li><label for="item_#{attribute}">#{attribute.titleize.capitalize} <small>#{link_to "Add new", { :controller => attribute.titleize.tableize, :action => 'new', :back_to => back_to, :selected => attribute }, :confirm => "Are you sure you want to leave this page?\nAny unsaved data will be lost." }</small></label>
  103. #{select :item, attribute, related.find(:all).collect { |p| [p.typus_name, p.id] }.sort_by { |e| e.first },{:prompt => "Select a #{related.name.downcase}"},{:disabled => attribute_disabled?(attribute)}}</li>
  104. HTML
  105. end
  106.  
  107. end
  108.  
  109. def typus_string_field(attribute, value)
  110.  
  111. # Read only fields.
  112. if @model.typus_field_options_for(:read_only).include?(attribute)
  113. value = 'read_only' if %w( edit ).include?(params[:action])
  114. end
  115.  
  116. # Auto generated fields.
  117. if @model.typus_field_options_for(:auto_generated).include?(attribute)
  118. value = 'auto_generated' if %w( new edit ).include?(params[:action])
  119. end
  120.  
  121. comment = %w( read_only auto_generated ).include?(value) ? (value + " field").titleize : ""
  122.  
  123. returning(String.new) do |html|
  124. html << <<-HTML
  125. <li><label for="item_#{attribute}">#{attribute.titleize.capitalize} <small>#{comment}</small></label>
  126. #{text_field :item, attribute, :class => 'text', :disabled => attribute_disabled?(attribute) }</li>
  127. HTML
  128. end
  129.  
  130. end
  131.  
  132. def typus_password_field(attribute, value)
  133. returning(String.new) do |html|
  134. html << <<-HTML
  135. <li><label for="item_#{attribute}">#{attribute.titleize.capitalize}</label>
  136. #{password_field :item, attribute, :class => 'text', :disabled => attribute_disabled?(attribute)}</li>
  137. HTML
  138. end
  139. end
  140.  
  141. def typus_boolean_field(attribute, value)
  142.  
  143. question = true if @model.typus_field_options_for(:questions).include?(attribute)
  144.  
  145. returning(String.new) do |html|
  146. html << <<-HTML
  147. <li><label for="item_#{attribute}">#{attribute.titleize.capitalize}#{'?' if question}</label>
  148. #{check_box :item, attribute} Checked if active</li>
  149. HTML
  150. end
  151.  
  152. end
  153.  
  154. def typus_file_field(attribute, value)
  155.  
  156. attribute_display = attribute.split("_file_name").first
  157. content_type = @item.send("#{attribute_display}_content_type")
  158.  
  159. returning(String.new) do |html|
  160.  
  161. html << <<-HTML
  162. <li><label for="item_#{attribute}">#{attribute_display.titleize.capitalize}</label>
  163. HTML
  164.  
  165. case content_type
  166. when /image/
  167. html << "#{link_to image_tag(@item.send(attribute_display).url(:thumb)), @item.send(attribute_display).url, :style => "border: 1px solid #D3D3D3;"}<br /><br />"
  168. when /pdf|flv|quicktime/
  169. html << "<p>No preview available. (#{content_type.split('/').last})</p>"
  170. end
  171.  
  172. html << "#{file_field :item, attribute.split("_file_name").first,:disabled => attribute_disabled?(attribute) }</li>"
  173.  
  174. end
  175.  
  176. end
  177.  
  178. def typus_form_has_many
  179.  
  180. back_to = "/" + ([] << params[:controller] << params[:id]<< params[:action]).compact.join('/')
  181.  
  182. returning(String.new) do |html|
  183.  
  184. @item_has_many.each do |field|
  185. html << <<-HTML
  186. <div class="box_relationships">
  187. <h2>
  188. #{link_to field.titleize, :controller => field}
  189. <small>#{link_to "Add new", :controller => field, :action => 'new', :back_to => back_to, :model => @model, :model_id => @item.id}</small>
  190. </h2>
  191. HTML
  192. @items = @model.find(params[:id]).send(field)
  193. unless @items.empty?
  194. html << build_table(@items[0].class, 'relationship', @items)
  195. else
  196. html << <<-HTML
  197. <div id="flash" class="notice"><p>There are no #{field.titleize.downcase}.</p></div>
  198. HTML
  199. end
  200. html << <<-HTML
  201. </div>
  202. HTML
  203. end
  204. end
  205. end
  206.  
  207. def typus_form_has_and_belongs_to_many
  208.  
  209. back_to = "/" + ([] << params[:controller] << params[:id]<< params[:action]).compact.join('/')
  210.  
  211. returning(String.new) do |html|
  212. @item_has_and_belongs_to_many.each do |field|
  213. model_to_relate = field.singularize.camelize.constantize
  214. html << <<-HTML
  215. <div class="box_relationships">
  216. <h2>
  217. #{link_to field.titleize, :controller => field}
  218. <small>#{link_to "Add new", :controller => field, :action => 'new', :back_to => back_to, :model => @model, :model_id => @item.id}</small>
  219. </h2>
  220. HTML
  221. items_to_relate = (model_to_relate.find(:all) - @item.send(field))
  222. unless items_to_relate.empty?
  223. html << <<-HTML
  224. #{form_tag :action => 'relate'}
  225. #{hidden_field :related, :model, :value => field.modelize}
  226. <p>#{ select :related, :id, items_to_relate.collect { |f| [f.typus_name, f.id] }.sort_by { |e| e.first } } &nbsp; #{submit_tag "Add", :class => 'button'}</form></p>
  227. HTML
  228. end
  229. current_model = @model.name.singularize.camelize.constantize
  230. @items = current_model.find(params[:id]).send(field)
  231. unless @items.empty?
  232. html << build_table(field.modelize, 'relationship')
  233. else
  234. html << <<-HTML
  235. <div id="flash" class="notice"><p>There are no #{field.titleize.downcase}.</p></div>
  236. HTML
  237. end
  238. html << <<-HTML
  239. </div>
  240. HTML
  241. end
  242. end
  243.  
  244. end
  245.  
  246. def attribute_disabled?(attribute)
  247. if @model.accessible_attributes.nil?
  248. return false
  249. else
  250. return !@model.accessible_attributes.include?(attribute)
  251. end
  252. end
  253.  
  254. ##
  255. # Tree when +acts_as_tree+
  256. #
  257. def expand_tree_into_select_field(categories)
  258. returning(String.new) do |html|
  259. categories.each do |category|
  260. html << %{<option #{"selected" if @item.parent_id == category.id} value="#{category.id}">#{"-" * category.ancestors.size} #{category.name}</option>}
  261. html << expand_tree_into_select_field(category.children) if category.has_children?
  262. end
  263. end
  264. end
  265.  
  266. end
Add Comment
Please, Sign In to add comment