module SciFormHelper class SciFormBuilder < ActionView::Helpers::FormBuilder # Returns Bootstrap date-time picker of the "datetime" type tailored for accessing a specified datetime attribute (identified by +name+) on an object # assigned to the template (identified by +object+). Additional options on the input tag can be passed as a # hash with +options+. The supported options are shown in the following examples. # # ==== Examples # Specify custom label (otherwise, a humanized version of +name+ is used) # # => datetime_picker(:post, :published, label: "Published date") # # Specify custom CSS style on the element # # => datetime_picker(:post, :published, style: "background-color: #000; margin-top: 20px;") # # Show a "clear" button on the bottom of the date picker. # # => datetime_picker(:post, :published, clear: true) # # Show a "today" button on the bottom of the date picker. # # => datetime_picker(:post, :published, today: true) def datetime_picker(name, options = {}) id = "#{@object_name}_#{name}" input_name = "#{@object_name}[#{name}]" value = options[:value] ? options[:value].strftime("#{I18n.backend.date_format} %H:%M") : '' js_locale = I18n.locale.to_s js_format = options[:time] ? datetime_picker_format_full : datetime_picker_format_date_only label = options[:label] || name.to_s.humanize style = options[:style] ? "style='#{options[:style]}'" : '' res = "
" \ "
" res << "
" if options[:clear] res << "" if options[:clear] res << "" \ "
" end res << '
' res.html_safe end # Returns Bootstrap button group for choosing a specified enum attribute (identified by +name+) on an object # assigned to the template (identified by +object+). Additional options on the input tag can be passed as a # hash with +options+. The supported options are shown in the following examples. # # ==== Examples # Specify custom label (otherwise, a humanized version of +name+ is used) # # => enum_btn_group(:car, :type, label: "Car type") # # Specify custom button names for enum values (a hash) # # => enum_btn_groups(:car, :type, btn_names: { diesel: "Diesel car", electric: "Electric car" }) # # Specify custom CSS style on the element # # => enum_btn_group(:car, :type, style: "background-color: #000; margin-top: 20px;") # # Specify custom CSS classes on the element # # => enum_btn_group(:car, :type, class: "class1 class2") def enum_btn_group(name, options = {}) id = "#{@object_name}_#{name}" input_name = "#{@object_name}[#{name}]" enum_vals = @object.class.send(name.to_s.pluralize) btn_names = enum_vals.keys.collect { |k| [k, k] }.to_h btn_names = options[:btn_names] if options[:btn_names] btn_names = ActiveSupport::HashWithIndifferentAccess.new(btn_names) label = name.to_s.humanize label = options[:label] if options[:label] style_str = '' style_str = " style='#{options[:style]}'" if options[:style] class_str = '' class_str = " #{options[:class]}" if options[:class] res = '' res << "
" res << "" res << '
' res << "
" enum_vals.keys.each do |val| active = @object.send("#{val}?") active_str = active ? ' active' : '' checked_str = active ? " checked='checked'" : '' res << "' end res << '
' res << '
' res.html_safe end # Returns smart