Class: Localized::ViewProperty
- Inherits:
-
Object
- Object
- Localized::ViewProperty
- Includes:
- ActionView::Helpers::FormHelper, ActionView::Helpers::FormTagHelper, ActionView::Helpers::TagHelper
- Defined in:
- lib/localized/view_property.rb
Instance Attribute Summary collapse
-
#column_name ⇒ Object
readonly
Returns the value of attribute column_name.
-
#master_class ⇒ Object
readonly
Returns the value of attribute master_class.
-
#master_hash ⇒ Object
readonly
Returns the value of attribute master_hash.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #blank? ⇒ Boolean
- #column_type ⇒ Object
- #has_column_type? ⇒ Boolean
- #has_format?(postfix = nil) ⇒ Boolean
- #has_master? ⇒ Boolean
- #has_time_format? ⇒ Boolean
- #human_edit(singular_name, view, opts = {}) ⇒ Object
-
#human_value(value, controller = nil) ⇒ Object
Rendering.
- #include_blank? ⇒ Boolean
-
#initialize(model, column_name, hash = nil) ⇒ ViewProperty
constructor
A new instance of ViewProperty.
- #klass ⇒ Object
- #master(value) ⇒ Object
- #masters ⇒ Object
- #reload_master ⇒ Object
- #system_column_type ⇒ Object
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_name ⇒ Object (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_class ⇒ Object (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_hash ⇒ Object (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 |
#model ⇒ Object (readonly)
Returns the value of attribute model.
2 3 4 |
# File 'lib/localized/view_property.rb', line 2 def model @model end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
2 3 4 |
# File 'lib/localized/view_property.rb', line 2 def @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
51 52 53 |
# File 'lib/localized/view_property.rb', line 51 def blank? @blank end |
#column_type ⇒ Object
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
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
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
101 102 103 |
# File 'lib/localized/view_property.rb', line 101 def has_master? not masters.empty? end |
#has_time_format? ⇒ 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}") = self..merge(opts) [:class] = "#{column_name} #{[: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, ) 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(" ") 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, ) when :radio, :radio_button separater = " " delimiter = " " html = masters.map{|key,val| [ view.(singular_name, column_name, key, ).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 = content_tag :div, html, :class=>"radio-group" html = content_tag :div, html, :class=>"fieldWithErrors" if record.errors.on(column_name) when :master html = view.collection_select(singular_name, column_name, masters, :first, :last, ) when :time tag = ActionView::Helpers::InstanceTag.new(singular_name, column_name, view) html = tag.to_time_select_tag() when NilClass if system_column_type tag = ActionView::Helpers::InstanceTag.new(singular_name, column_name, view, view, record) html = tag.to_tag() 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() 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 = content_tag(:span, title || name) content_tag(:li, span, :class=>style) } return content_tag(:ul, lis.join(' '), :class=>"aab") when :checkbox = (||{}).merge(:disabled=>"disabled") html = masters.map{|(name, title)| check = check_box_tag("aab", 1, (checkeds[name].to_i==1), ) label = h(title || name) '<span style="white-space: nowrap;">%s %s</span>' % [check, label] }.join(" ") 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
105 106 107 |
# File 'lib/localized/view_property.rb', line 105 def include_blank? @include_blank end |
#klass ⇒ Object
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 |
#masters ⇒ Object
88 89 90 |
# File 'lib/localized/view_property.rb', line 88 def masters @master_array end |
#reload_master ⇒ Object
96 97 98 99 |
# File 'lib/localized/view_property.rb', line 96 def reload_master parse_master return masters end |
#system_column_type ⇒ Object
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 |