require "action_view" require "action_view/helpers" require "action_view/helpers/auto_tag_helper/version" require "action_view/helpers/auto_tag_helper/form_info" require "action_view/helpers/auto_tag_helper/display_types" module ActionView module Helpers module AutoTagHelper # == Parameters: # method_name:: # called method name # rec:: # object which is called method # html_options:: # html options , ex: {style: "width: 100px; height: 100px; color: #ff0000;" placeholder: 'your name'} # def display_tag(method_name,rec,html_options={}) rec.class.try(:reflections).try(:each) do |reflname,refl| if refl.foreign_key.to_sym == method_name.to_sym && refl.macro == :belongs_to method_name = reflname break end end v = rec.__send__(method_name) display_tag_with_display_options(v,rec,method_name,html_options) || v.try(:__display__) || v.to_s end def display_tag_with_display_options(v,rec,method_name,html_options={}) if !rec.class.respond_to? :display_options return nil end opts = (rec.class.display_options(method_name) || {}).symbolize_keys opt_type = opts[:type] opt_method = opts[:method] display_type = ActionView::Helpers::AutoTagHelper::DisplayTypes[opt_type] if !(opt_method || display_type) return nil end args = (opts[:args] || [v]) if args.kind_of? Proc args = args.call(v,rec) end if !args.kind_of? Array args = [args] end if opt_method @tag_options = html_options r = self.__send__(opt_method,*args) @tag_options = nil r else display_type.display_tag(v,html_options) end end def auto_input_tag(sym,model,name,value) column = model.columns_hash[sym.to_s] type = column.type opts = model.try(:input_options,sym) || {} reference_tag(sym,model,name,value) || boolean_tag(type,name,value) || datetime_tag(type,name,value,opts) || number_tag(type,name,value,opts) || text_tag(type,name,value,opts) end def reference_tag(sym,model,name,value) if !model.respond_to? :select_list return nil end rtn = nil column = model.columns_hash[sym.to_s] if list = model.select_list(sym) rtn = select_tag(name,options_from_collection_for_select(list, :id, :__display__, value.to_s),include_blank: column.null) end rtn end def boolean_tag(type,name,value) if type.to_sym == :boolean check_box_tag(name,value="1",value) else nil end end # type == :integer # [:range :week , :month] def number_tag(type,name,value,opts={}) case type.to_sym when :integer opts = opts.stringify_keys case (opts["type"] || type).to_sym when :range range_field_tag(name, value, opts) when :week,:month text_field_tag(name, value, opts) else number_field_tag(name, value, opts) end when :float,:decimal number_field_tag(name, value, opts) else nil end end # type == :datetime # datetime-local def datetime_tag(type,name,value,opts={}) type = type.to_sym case type when :datetime opts = opts.stringify_keys opts["type"] ||= type.to_s text_field_tag(name,value,opts) when :time,:date text_field_tag(name,value,opts.merge(type: type)) else nil end end # type == :string # input_type: [:color, :email, :number, :password, :tel, :url] def text_tag(type,name,value,opts={}) case type.to_sym when :text text_area_tag(name,value) when :string text_field_tag(name,value,opts) else nil end end end include AutoTagHelper include FormTagHelper def tag_options(options, escape = true) if @tag_options.is_a?(Hash) options = options.merge(@tag_options) @tag_options = nil end super(options, escape) end end end