require "we_bridge/auto_view_helper/version" require "actionview/helpers/auto_tag_helper" module WeBridge module AutoViewHelper include ActionView::Helpers::AutoTagHelper def t_xxx(xxx,k) ::I18n.t("db.#{xxx}.#{k}",default: ::I18n.t("db._common_.#{k}",default: k.to_s)) end def self.default_options @default_options ? @default_options.dup : {show_path: nil, edit_path: nil, new_path: nil, back_path: nil, root_path: '/', locale: ::I18n.default_locale} end def self.default_options=(options) @default_options = options end def edit_common(record,url,options={}) self._form_common(record,url,options) end def new_common(record,url,options={}) self._form_common(record,url,{title: "New " + record.class.name }.merge(options)) end def _form_common(record,url,options={}) options = WeBridge::AutoViewHelper.default_options.merge options.symbolize_keys show_path = options[:show_path] root_path = options[:root_path] back_path = options[:back_path] title = options[:title] locale = options[:locale] title ||= "Edit " + record.class.name method ||= nil markup do |m| m.section do m.h2 title m << form_tag(url, method: method || :patch, multipart: true) do markup do |m2| m2.dl do klass = record.class syms = [] if klass.respond_to? :editable_columns syms = klass.editable_columns else syms = klass.content_columns.map(&:name) end syms.each do |sym| m2.dt t_xxx(record.class.table_name,sym) m2.dd { m2 << auto_input_tag(sym,record.class,"#{record.class.table_name}[#{sym}]",record.__send__(sym)) } end m2.div class: locale do if record.respond_to?(:text) && record.respond_to?(:text) && record.respond_to?(:texts) && (text = record.text(locale)) syms = [] if text.respond_to? :editable_columns syms = text.editable_columns else syms = text.content_columns.map(&:name) end syms.each do |sym| column = text.class.columns_hash[sym.to_s] m2.dt t_xxx(text.class.table_name,sym) m2.dd { m2 << text_tag(column.type,"#{text.class.table_name}[#{locale}][#{sym}]",text.__send__(sym)) } end end end end m2 << submit_tag("Save") end.html_safe end end if show_path m.div do m.a "Show", href: show_path end end if back_path m.div do m.a "Back", href: back_path end end if root_path m.div do m.a "Top", href: root_path end end end end def show_common(record,options={}) options = WeBridge::Common::Layouts.default_options.merge options.symbolize_keys edit_path = options[:edit_path] root_path = options[:root_path] markup do |m| m.section do m.h2 "show " m << self.show_record(record) if edit_path m.div do m.a "Edit", href: edit_path end end if root_path m.div do m.a "Top", href: root_path end end end end end def show_record(record) model = record.class markup do |m| m.dl class: :table do method_names = model.respond_to?(:showable_methods) ? model.showable_methods : model.content_columns.map(&:name) method_names.each do |mn| m.dt t_xxx(record.class.table_name,mn) m.dd { m << display_tag(mn,record) } end if record.respond_to?(:text) && model.respond_to?(:text) text_model = model.text if text_model.respond_to? :showable_methods mns = text_model.showable_methods lang_model = defined?(::Lang) ? ::Lang : ActiveRecord::Mlang::Lang langs = lang_model.all.to_a langs.each do |lang| text = record.text(lang.code) mns.each do |mn| m.div class: lang.code do m.dt t_xxx(record.class.text.table_name,mn) m.dd { m << (show_tag(mn,text) || "N/A") } end end end end end end end end end end ActionView::Base.__send__(:include,WeBridge::AutoViewHelper)