Module: WeBridge::AutoViewHelper

Includes:
ActionView::Helpers::AutoTagHelper
Defined in:
lib/we_bridge/auto_view_helper.rb,
lib/we_bridge/auto_view_helper/version.rb,
lib/we_bridge/auto_view_helper/controller.rb

Defined Under Namespace

Modules: Controller

Constant Summary collapse

VERSION =
"0.0.10"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_optionsObject



11
12
13
# File 'lib/we_bridge/auto_view_helper.rb', line 11

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

.default_options=(options) ⇒ Object



15
16
17
# File 'lib/we_bridge/auto_view_helper.rb', line 15

def self.default_options=(options)
  @default_options = options
end

Instance Method Details

#_form_common(record, url, options = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/we_bridge/auto_view_helper.rb', line 72

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

#edit_common(record, url, options = {}) ⇒ Object



64
65
66
# File 'lib/we_bridge/auto_view_helper.rb', line 64

def edit_common(record,url,options={})
  self._form_common(record,url,{method: :patch}.merge(options))
end

#index_common(records, options) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/we_bridge/auto_view_helper.rb', line 19

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 " + model.try(:table_name).to_s, new_path)
      end
    end
  end
end

#new_common(record, url, options = {}) ⇒ Object



68
69
70
# File 'lib/we_bridge/auto_view_helper.rb', line 68

def new_common(record,url,options={})
  self._form_common(record,url,{title: "New " + record.class.name }.merge(options))
end

#show_common(record, options = {}) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/we_bridge/auto_view_helper.rb', line 145

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

#show_record(record) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/we_bridge/auto_view_helper.rb', line 178

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

#show_record_text(record) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/we_bridge/auto_view_helper.rb', line 191

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

#t_xxx(xxx, k) ⇒ Object



7
8
9
# File 'lib/we_bridge/auto_view_helper.rb', line 7

def t_xxx(xxx,k)
  ::I18n.t("db.#{xxx}.#{k}",default: ::I18n.t("db._common_.#{k}",default: k.to_s))
end