rearrange some things and add a table builder

This commit is contained in:
John Bintz 2012-12-06 11:03:45 -05:00
parent 40b24799af
commit 6f1c3a608f
4 changed files with 83 additions and 51 deletions

View File

@ -1,56 +1,7 @@
require 'delegate'
require 'semantic_rails_view_helpers/attributes_builder_base'
module SemanticRailsViewHelpers
class AttributesBuilder < SimpleDelegator
attr_reader :object
def initialize(object, context, block)
@object, @context, @block = object, context, block
end
def __getobj__
@object
end
def to_s
@context.capture(self, &@block)
end
def field(field, options = {}, &block)
if options[:value]
raw_value = options[:value]
else
raw_value = @object.send(field)
if block
raw_value = block.call(raw_value)
end
end
value = raw_value
value = value.to_label if value.respond_to?(:to_label)
value = @context.content_tag(:data, value, 'data-field' => field)
if options[:as]
value = @context.render(:partial => "attributes/#{options[:as]}", :locals => { :object => @object, :field => field, :raw_value => raw_value, :value => value })
end
(value or '').html_safe
end
class TagBuilder
def initialize(attributes_builder)
@attributes_builder = attributes_builder
end
def method_missing(tag, field, options = {})
@attributes_builder.context.content_tag(tag, )
end
end
def tag
TagBuilder.new(self)
end
class AttributesBuilder < AttributesBuilderBase
end
end

View File

@ -0,0 +1,56 @@
require 'delegate'
module SemanticRailsViewHelpers
class AttributesBuilderBase < SimpleDelegator
attr_reader :object
def initialize(object, context, block)
@object, @context, @block = object, context, block
end
def __getobj__
@object
end
def to_s
@context.capture(self, &@block)
end
def field(field, options = {}, &block)
if options[:value]
raw_value = options[:value]
else
raw_value = @object.send(field)
if block
raw_value = block.call(raw_value)
end
end
value = raw_value
value = value.to_label if value.respond_to?(:to_label)
value = @context.content_tag(:data, value, 'data-field' => field)
if options[:as]
value = @context.render(:partial => "attributes/#{options[:as]}", :locals => { :object => @object, :field => field, :raw_value => raw_value, :value => value })
end
(value or '').html_safe
end
class TagBuilder
def initialize(attributes_builder)
@attributes_builder = attributes_builder
end
def method_missing(tag, field, options = {})
@attributes_builder.context.content_tag(tag)
end
end
def tag
TagBuilder.new(self)
end
end
end

View File

@ -0,0 +1,20 @@
require 'semantic_rails_view_helpers/attributes_builder_base'
module SemanticRailsViewHelpers
class AttributesTableBuilder < AttributesBuilderBase
def to_s
@context.content_tag(:table) do
@context.capture(self, &@block)
end
end
def field(field, options = {}, &block)
value = super
@context.content_tag(:tr) do
@context.content_tag(:th, @context.t(".#{field}")) << @context.content_tag(:td, value)
end.html_safe
end
end
end

View File

@ -1,4 +1,5 @@
require 'semantic_rails_view_helpers/attributes_builder'
require 'semantic_rails_view_helpers/attributes_table_builder'
module SemanticRailsViewHelpers
module ViewHelpers
@ -6,6 +7,10 @@ module SemanticRailsViewHelpers
AttributesBuilder.new(object, self, block)
end
def attributes_table_for(object, &block)
AttributesTableBuilder.new(object, self, block)
end
def link_to_route(route, *args)
link_to t(".#{route}"), send("#{route}_path", *args), 'data-link' => route
end