Class: Localized::ViewProperty

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::FormHelper, ActionView::Helpers::FormTagHelper, ActionView::Helpers::TagHelper
Defined in:
lib/localized/view_property.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, column_name, hash = nil) ⇒ ViewProperty

Returns a new instance of ViewProperty.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/localized/view_property.rb', line 35

def initialize(model, column_name, hash = nil)
  hash ||= {}

  @blank        = true if hash.blank?
  @model        = model
  @column_name  = column_name
  @yaml_data    = hash
  @options      = hash[:options] || {}
  @master_array = []
  @master_hash  = {}
  @master_class = nil

  parse_master
  @include_blank = @master_array.first.first.to_s.empty? rescue false
end

Instance Attribute Details

#column_nameObject (readonly)

Returns the value of attribute column_name.



2
3
4
# File 'lib/localized/view_property.rb', line 2

def column_name
  @column_name
end

#master_classObject (readonly)

Returns the value of attribute master_class.



2
3
4
# File 'lib/localized/view_property.rb', line 2

def master_class
  @master_class
end

#master_hashObject (readonly)

Returns the value of attribute master_hash.



2
3
4
# File 'lib/localized/view_property.rb', line 2

def master_hash
  @master_hash
end

#modelObject (readonly)

Returns the value of attribute model.



2
3
4
# File 'lib/localized/view_property.rb', line 2

def model
  @model
end

#optionsObject (readonly)

Returns the value of attribute options.



2
3
4
# File 'lib/localized/view_property.rb', line 2

def options
  @options
end

Class Method Details

.[](model_name, column_name) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/localized/view_property.rb', line 5

def [](model_name, column_name)
  model = find_model(model_name)
  if model
    model.view_property(column_name)
  else
    nil
  end
end

Instance Method Details

#[](key) ⇒ Object



84
85
86
# File 'lib/localized/view_property.rb', line 84

def [](key)
  @yaml_data[key]
end

#blank?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/localized/view_property.rb', line 51

def blank?
  @blank
end

#column_typeObject



121
122
123
# File 'lib/localized/view_property.rb', line 121

def column_type
  (has_column_type? && self[:column_type]) || (has_master? && :master) || nil
end

#has_column_type?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/localized/view_property.rb', line 117

def has_column_type?
  self[:column_type].is_a?(Symbol)
end

#has_format?(postfix = nil) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/localized/view_property.rb', line 113

def has_format?(postfix = nil)
  self["format#{postfix}".intern] || self[:format]
end

#has_master?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/localized/view_property.rb', line 101

def has_master?
  not masters.empty?
end

#has_time_format?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/localized/view_property.rb', line 109

def has_time_format?
  self[:time_format]
end

#human_edit(singular_name, view, opts = {}) ⇒ 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/localized/view_property.rb', line 191

def human_edit(singular_name, view, opts = {})
  record  = opts.delete(:record) || view.instance_variable_get("@#{singular_name}")
  options = self.options.merge(opts)
  options[:class] = "#{column_name} #{options[:class]}".strip

  if time_format = has_time_format?
    value = record.send(column_name)
    if edit_format = has_format?("_edit")
      return human_edit_time_text_field_with_format(view, edit_format, value, singular_name)
    else
      return human_edit_time_with_format(view, time_format, value, singular_name)
    end
  end

  case column_type
  when :acts_as_bits
    aab_masters = self.masters
    aab_masters = klass.send("#{column_name.to_s.singularize}_names_with_labels") if aab_masters.blank?

    html = aab_masters.map{|(name, title)|
      check = view.check_box(singular_name, name, options) rescue "(#{name}?)"
      label = view.send(:h, title || name)
      label = view.send(:content_tag, :label, label, :for=>"#{singular_name}_#{name}")
      '<span style="white-space: nowrap;">%s %s</span>' % [check, label]
    }.join(" &nbsp;&nbsp; ")
  when :acts_as_tree
    value  = record.send(column_name)
    record = master_class.find(value) rescue nil
    html   = view.acts_as_tree_field(singular_name, column_name, master_class, record)
  when :checkbox, :check_box
    html = view.check_box(singular_name, column_name, options)
  when :radio, :radio_button
    separater = "&nbsp;"
    delimiter = "&nbsp;&nbsp;"
    html = masters.map{|key,val|
      [
       view.radio_button(singular_name, column_name, key, options).to_s,
       # content_tag(:label, val.to_s, :for=>"#{singular_name}_#{column_name}_#{key}")
       val.to_s
      ].join(separater)
    } * delimiter

    # add following line to your css to avoid blocked radio button in AR error
    # div.fieldWithErrors .radio-group {border:1px solid #FF0000;}
    # .radio-group div.fieldWithErrors {display : inline;}

    html =  :div, html, :class=>"radio-group"
    html =  :div, html, :class=>"fieldWithErrors" if record.errors.on(column_name)

  when :master
    html = view.collection_select(singular_name, column_name, masters, :first, :last, options)
  when :time
    tag  = ActionView::Helpers::InstanceTag.new(singular_name, column_name, view)
    html = tag.to_time_select_tag(options)
  when NilClass
    if system_column_type
      tag  = ActionView::Helpers::InstanceTag.new(singular_name, column_name, view, view, record)
      html = tag.to_tag(options)
    else
      if view.respond_to?(column_name)
        view.send(column_name, record)
      else
        view.text_field_tag "#{singular_name}[#{column_name}]", record[column_name]
      end
    end
  else
    tag = ActionView::Helpers::InstanceTag.new(singular_name, column_name, view, view, record)
    tag.instance_eval("def column_type; :%s; end" % self[:column_type])
    html = tag.to_tag(options)
  end

#       if format = has_format?("_" + view.controller.action_name)
#         html = format % html
#       end

  return html
end

#human_value(value, controller = nil) ⇒ Object

Rendering



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
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/localized/view_property.rb', line 146

def human_value (value, controller = nil)
  controller &&= controller.is_a?(ActionController::Base) ? controller : controller.controller

  case column_type
  when :acts_as_bits
    aab_names = klass.send("#{column_name.to_s.singularize}_names")
    checkeds  = Hash[*aab_names.zip(value.to_s.split(//)).flatten]

    aab_masters = self.masters
    aab_masters = klass.send("#{column_name.to_s.singularize}_names_with_labels") if aab_masters.blank?

    type = :button
    case type
    when :button
      lis = aab_masters.map{|(name, title)|
        style = (checkeds[name].to_i == 1) ? "checked" : "unchecked"
        span  = (:span, title || name)
        (:li, span, :class=>style)
      }
      return (:ul, lis.join(' '), :class=>"aab")
    when :checkbox
      options = (options||{}).merge(:disabled=>"disabled")
      html = masters.map{|(name, title)|
        check = check_box_tag("aab", 1, (checkeds[name].to_i==1), options)
        label = h(title || name)
        '<span style="white-space: nowrap;">%s %s</span>' % [check, label]
      }.join(" &nbsp;&nbsp; ")
      return html
    end
  end

  if has_master?
    html = master(value)
  elsif has_time_format?
    html = [Date, Time].include?(value.class) ? value.strftime(self[:time_format]) : ''
  elsif controller && format = has_format?("_" + controller.action_name)
    html = format % ERB::Util.html_escape(value)
  elsif (column_type || system_column_type) == :text
    html = ERB::Util.html_escape(value.strip.to_s).gsub(/\r?\n/,'<BR>')
  else
    html = ERB::Util.html_escape(value)
  end
end

#include_blank?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/localized/view_property.rb', line 105

def include_blank?
  @include_blank
end

#klassObject



132
133
134
# File 'lib/localized/view_property.rb', line 132

def klass
  model.active_record
end

#master(value) ⇒ Object



92
93
94
# File 'lib/localized/view_property.rb', line 92

def master(value)
  @master_hash[value]
end

#mastersObject



88
89
90
# File 'lib/localized/view_property.rb', line 88

def masters
  @master_array
end

#reload_masterObject



96
97
98
99
# File 'lib/localized/view_property.rb', line 96

def reload_master
  parse_master
  return masters
end

#system_column_typeObject



125
126
127
128
129
130
# File 'lib/localized/view_property.rb', line 125

def system_column_type
  column = klass.columns_hash[column_name.to_s]
  column ? column.type : nil
rescue
  nil
end