enable turning on/off semantic data helpers
This commit is contained in:
parent
d288d62f9e
commit
6ae523a21d
|
@ -1,3 +1,14 @@
|
|||
require "semantic_rails_view_helpers/version"
|
||||
require 'semantic_rails_view_helpers/engine' if defined?(Rails::Engine)
|
||||
|
||||
module SemanticRailsViewHelpers
|
||||
def self.semantic_data?
|
||||
Rails.configuration.add_semantic_data
|
||||
end
|
||||
|
||||
def self.with_semantic_data
|
||||
return {} if !semantic_data?
|
||||
|
||||
yield
|
||||
end
|
||||
end
|
||||
|
|
|
@ -29,7 +29,10 @@ module SemanticRailsViewHelpers
|
|||
|
||||
value = raw_value
|
||||
value = value.to_label if value.respond_to?(:to_label)
|
||||
value = @context.content_tag(:data, value, 'data-field' => field)
|
||||
|
||||
if SemanticRailsViewHelpers.semantic_data?
|
||||
value = @context.content_tag(:data, value, 'data-field' => field)
|
||||
end
|
||||
|
||||
if options[:as]
|
||||
value = @context.render(:partial => "attributes/#{options[:as]}", :locals => { :object => @object, :field => field, :raw_value => raw_value, :value => value })
|
||||
|
|
|
@ -2,8 +2,17 @@ require 'semantic_rails_view_helpers/attributes_builder_base'
|
|||
|
||||
module SemanticRailsViewHelpers
|
||||
class AttributesTableBuilder < AttributesBuilderBase
|
||||
def initialize(object, options, context, block)
|
||||
super(object, context, block)
|
||||
|
||||
@options = options
|
||||
end
|
||||
|
||||
def to_s
|
||||
@context.content_tag(:table) do
|
||||
@options[:class] ||= ''
|
||||
@options[:class] << 'attributes table'
|
||||
|
||||
@context.content_tag(:table, @options) do
|
||||
@context.capture(self, &@block)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,8 +2,10 @@ require 'semantic_rails_view_helpers/view_helpers'
|
|||
|
||||
module SemanticRailsViewHelpers
|
||||
class Engine < ::Rails::Engine
|
||||
initializer 'semantic_rails_view_helpers.initialize' do
|
||||
initializer 'semantic_rails_view_helpers.initialize' do |app|
|
||||
ActionView::Base.send :include, SemanticRailsViewHelpers::ViewHelpers
|
||||
|
||||
app.config.add_semantic_data = false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,25 +7,25 @@ module SemanticRailsViewHelpers
|
|||
AttributesBuilder.new(object, self, block)
|
||||
end
|
||||
|
||||
def attributes_table_for(object, &block)
|
||||
AttributesTableBuilder.new(object, self, block)
|
||||
def attributes_table_for(object, options = {}, &block)
|
||||
AttributesTableBuilder.new(object, options, self, block)
|
||||
end
|
||||
|
||||
def link_to_route(route, *args)
|
||||
link_to t(".#{route}"), send("#{route}_path", *args), 'data-link' => route
|
||||
link_to t(".#{route}"), send("#{route}_path", *args), semantic_link_data(route)
|
||||
end
|
||||
|
||||
def link_to_collection(route)
|
||||
collection = route.last
|
||||
|
||||
link_to t(".#{collection}"), polymorphic_url(route), 'data-link' => collection
|
||||
link_to t(".#{collection}"), polymorphic_url(route), semantic_link_data(collection)
|
||||
end
|
||||
|
||||
def link_to_model(model)
|
||||
route = model
|
||||
route = route.to_route if route.respond_to?(:to_route)
|
||||
|
||||
link_to model.to_label, polymorphic_url(route), 'data-action' => 'show'
|
||||
link_to model.to_label, polymorphic_url(route), semantic_model_data(model).merge(semantic_action_data('show'))
|
||||
end
|
||||
|
||||
def link_to_model_action(model, action = :show, options = {})
|
||||
|
@ -47,19 +47,38 @@ module SemanticRailsViewHelpers
|
|||
route = model
|
||||
route = route.to_route if route.respond_to?(:to_route)
|
||||
|
||||
link_to label, polymorphic_url(route, :action => action), options.merge("data-action" => target_action)
|
||||
link_to label, polymorphic_url(route, :action => action), options.merge(semantic_action_data(target_action))
|
||||
end
|
||||
|
||||
def li_for(object, options = {}, &block)
|
||||
type = begin
|
||||
if object.respond_to?(:model)
|
||||
object.model.class
|
||||
else
|
||||
object.class
|
||||
end
|
||||
end
|
||||
content_tag(:li, capture(&block), options.merge(semantic_model_data(object)))
|
||||
end
|
||||
|
||||
content_tag(:li, capture(&block), options.merge('data-type' => type, 'data-id' => object.id))
|
||||
private
|
||||
def semantic_model_data(object)
|
||||
SemanticRailsViewHelpers.with_semantic_data do
|
||||
type = begin
|
||||
if object.respond_to?(:model)
|
||||
object.model.class
|
||||
else
|
||||
object.class
|
||||
end
|
||||
end
|
||||
|
||||
{ 'data-type' => type, 'data-id' => object.id }
|
||||
end
|
||||
end
|
||||
|
||||
def semantic_action_data(action)
|
||||
SemanticRailsViewHelpers.with_semantic_data do
|
||||
{ "data-action" => target_action }
|
||||
end
|
||||
end
|
||||
|
||||
def semantic_link_data(link)
|
||||
SemanticRailsViewHelpers.with_semantic_data do
|
||||
{ 'data-link' => link }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue