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 index_common records,options options = WeBridge::AutoViewHelper.default_options.merge options.symbolize_keys show_path = options[:show_path] edit_path = options[:edit_path] new_path = options[:new_path] destroy_path = options[:destroy_path] || show_path root_path = options[:root_path] back_path = options[:back_path] title = options[:title] locale = options[:locale] model = options[:model] || records.try(:first).try(:class) markup do |m| m.h1 title || 'Listing ' + model.try(:table_name).to_s m.table do m.thead do m.tr do m.th m.th m.th m.th end end m.tbody do records.each do |record| m.tr do m.td record.try(:__display__) || record.to_s m.td { m << link_to('Show', show_path.try(:call, record) || show_path) } m.td { m << link_to('Edit', edit_path.try(:call, record) || edit_path) } m.td { m << link_to('Destroy', destroy_path.try(:call, record) || destroy_path, data: { confirm: 'Are you sure?' }, method: :delete) } end end end end if new_path m.div do m << link_to('New Lang', new_path) end end end end def edit_common(record,url,options={}) self._form_common(record,url,{method: :patch}.merge(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 = options[:method] klass = record.class markup do |m| m.section do m.h2 title m << form_tag(url, method: method || :post, multipart: true) do markup do |m2| m2.dl do syms = klass.try(:editable_columns) || klass.try(:content_columns).try(:map, &:name) || [] 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 end if text_class = klass.try(:text) text_table_name = text_class.table_name lang_model = defined?(::Lang) ? ::Lang : ActiveRecord::Mlang::Lang langs = lang_model.all.to_a langs.each do |lang| m2.fieldset class: lang.code do m2.legend lang.code syms = text_class.try(:editable_columns) || text_class.try(:content_columns).try(:map, &:name) || [] text = record.try(:text,lang.code) || text_class.new m2.dl do syms.each do |sym| column = text_class.columns_hash[sym.to_s] m2.dt t_xxx(text_table_name,sym) m2.dd { m2 << auto_input_tag(sym,text_class,"#{text_table_name}[#{lang.code}][#{sym}]",text.__send__(sym)) } end 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::AutoViewHelper.default_options.merge options.symbolize_keys edit_path = options[:edit_path] back_path = options[:back_path] root_path = options[:root_path] markup do |m| m.section do m.h2 "show " m << self.show_record(record) m << self.show_record_text(record) if edit_path m.div do m.a "Edit", href: edit_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 end def show_record(record) model = record.class markup do |m| m.dl class: :table do method_names = model.try(: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 end end end def show_record_text(record) model = record.class if !((text_model = model.try(:text)) && record.respond_to?(:text)) return "" end mns = text_model.try(:showable_methods) || text_model.content_columns.map(&:name) || [] if !mns.present? return "" end lang_model = defined?(::Lang) ? ::Lang : ActiveRecord::Mlang::Lang langs = lang_model.all.to_a markup do |m| langs.each do |lang| text = record.text(lang.code) m.fieldset class: lang.code do m.legend lang.code m.dl do mns.each do |mn| m.dt t_xxx(text_model.table_name,mn) m.dd { m << (display_tag(mn,text) || "N/A") } end end end end end end end end ActionView::Base.__send__(:include,WeBridge::AutoViewHelper)