lib/iord/fields.rb in iord-1.0.3 vs lib/iord/fields.rb in iord-1.1.0
- old
+ new
@@ -12,18 +12,17 @@
helper_method :field_value
helper_method :iordh
end
- attr_writer :o
- def o
- @o ||= ::Iord::OutputHelper.new(view_context)
+ def iordh
+ @iordh ||= OutputHelper.new(view_context)
end
- alias_method :iordh, :o
+ # Use for sort_if_enabled which requires the attribute name
def field_attribute(attr)
- return "id" if attr == :_id
+ return 'id' if attr == :_id
# default, simply return name
return attr unless attr.is_a? Hash
# else, Hash
return attr[:object] if attr.has_key? :object
return attr[:array] if attr.has_key? :array
@@ -34,43 +33,38 @@
end
def field_name(resource, attr = nil)
attr = resource if attr.nil?
- return "id" if attr == :_id
+ return 'id' if attr == :_id
# default, simply return name
return attr unless attr.is_a? Hash
# else, Hash
return attr[:as] if attr.has_key? :as
return attr[:object] if attr.has_key? :object
return attr[:array] if attr.has_key? :array
return attr[:value] if attr.has_key? :value
- if attr.has_key? :link
- if attr[:link].respond_to? :call
- return attr[:link].call(resource, attr)
- else
- return attr[:link]
- end
- end
+ link = attr[:link]
+ link = link.call(resource, attr) if link.respond_to? :call
- attr.keys[0]
+ link || attr.keys[0]
end
def field_label(f, attr)
return f.label *attr[:as] if attr.has_key? :as
f.label attr[:attr]
end
def field_form(f, attr)
if attr.is_a? Symbol
- return o.input(f.label(attr), f.text_field(attr), f.object.errors.full_messages_for(attr))
+ return iordh.input(f.label(attr), f.text_field(attr), f.object.errors.full_messages_for(attr))
elsif attr.is_a? Array
label = f.label attr[1]
field = f.public_send *attr
errors = f.object.errors.full_messages_for(attr[1])
- return o.input(label, field, errors)
+ return iordh.input(label, field, errors)
elsif not attr.is_a? Hash
raise ArgumentError, "Unrecognized attr: #{attr}"
elsif attr.has_key? :fields
return field_form_object(f, attr)
else
@@ -91,11 +85,11 @@
return field if attr.has_key? :hidden
label = field_label(f, attr)
errors = f.object.errors.full_messages_for(attr[:attr])
- o.input(label, field, errors)
+ iordh.input(label, field, errors)
end
def field_form_object(f, attr)
multiple_items = attr[:attr].to_s.pluralize == attr[:attr].to_s
@@ -107,18 +101,18 @@
attr.is_a? Hash and
attr.has_key? :not_new_record)
end
html = f.fields_for(attr[:attr]) do |ff|
- o.fieldset(attr_name, attr[:fields], ff, predicate) do
+ iordh.fieldset(attr_name, attr[:fields], ff, predicate) do
if multiple_items
- o.link_to_remove(ff, attr_name)
+ iordh.link_to_remove(ff, attr_name)
end
end
end
if multiple_items
- html += o.link_to_add(f, attr_name, attr[:attr])
+ html += iordh.link_to_add(f, attr_name, attr[:attr])
end
html.nil? ? String.new : html.html_safe
end
def field_value(resource, attr)
@@ -134,26 +128,26 @@
end
return resource.public_send(attr) unless attr.is_a? Hash
# complex value with Hash
if attr.has_key? :array
- o.display_array(resource.public_send(attr[:array]), attr[:attr][:attrs])
+ iordh.display_array(resource.public_send(attr[:array]), attr[:attrs])
elsif attr.has_key? :object
- o.display(resource.public_send(attr[:object]), attr[:attrs])
+ iordh.display(resource.public_send(attr[:object]), attr[:attrs])
elsif attr.has_key? :value
if resource.respond_to? attr[:value]
attr[:format].call(resource.public_send(attr[:value]))
else
attr[:format].call(resource, attr[:value])
end
elsif attr.has_key? :image
- o.image resource.public_send(*attr[:image]), attr[:params]
+ iordh.image resource.public_send(*attr[:image]), attr[:params]
elsif attr.has_key? :link
label, url = attr[:label], attr[:url]
- label = attr[:label].call(resource, attr) if attr[:label].respond_to? :call
- url = attr[:url].call(resource, attr) if attr[:url].respond_to? :call
+ label = label.call(resource, attr) if label.respond_to? :call
+ url = url.call(resource, attr) if url.respond_to? :call
- o.link_to label, url, attr[:params]
+ iordh.link_to label, url, attr[:params]
else
field_value(resource.public_send(attr.keys[0]), attr.values[0])
end
end
end