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/version"
|
||||||
require 'semantic_rails_view_helpers/engine' if defined?(Rails::Engine)
|
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 = raw_value
|
||||||
value = value.to_label if value.respond_to?(:to_label)
|
value = value.to_label if value.respond_to?(:to_label)
|
||||||
|
|
||||||
|
if SemanticRailsViewHelpers.semantic_data?
|
||||||
value = @context.content_tag(:data, value, 'data-field' => field)
|
value = @context.content_tag(:data, value, 'data-field' => field)
|
||||||
|
end
|
||||||
|
|
||||||
if options[:as]
|
if options[:as]
|
||||||
value = @context.render(:partial => "attributes/#{options[:as]}", :locals => { :object => @object, :field => field, :raw_value => raw_value, :value => value })
|
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
|
module SemanticRailsViewHelpers
|
||||||
class AttributesTableBuilder < AttributesBuilderBase
|
class AttributesTableBuilder < AttributesBuilderBase
|
||||||
|
def initialize(object, options, context, block)
|
||||||
|
super(object, context, block)
|
||||||
|
|
||||||
|
@options = options
|
||||||
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
@context.content_tag(:table) do
|
@options[:class] ||= ''
|
||||||
|
@options[:class] << 'attributes table'
|
||||||
|
|
||||||
|
@context.content_tag(:table, @options) do
|
||||||
@context.capture(self, &@block)
|
@context.capture(self, &@block)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,8 +2,10 @@ require 'semantic_rails_view_helpers/view_helpers'
|
||||||
|
|
||||||
module SemanticRailsViewHelpers
|
module SemanticRailsViewHelpers
|
||||||
class Engine < ::Rails::Engine
|
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
|
ActionView::Base.send :include, SemanticRailsViewHelpers::ViewHelpers
|
||||||
|
|
||||||
|
app.config.add_semantic_data = false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,25 +7,25 @@ module SemanticRailsViewHelpers
|
||||||
AttributesBuilder.new(object, self, block)
|
AttributesBuilder.new(object, self, block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def attributes_table_for(object, &block)
|
def attributes_table_for(object, options = {}, &block)
|
||||||
AttributesTableBuilder.new(object, self, block)
|
AttributesTableBuilder.new(object, options, self, block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def link_to_route(route, *args)
|
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
|
end
|
||||||
|
|
||||||
def link_to_collection(route)
|
def link_to_collection(route)
|
||||||
collection = route.last
|
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
|
end
|
||||||
|
|
||||||
def link_to_model(model)
|
def link_to_model(model)
|
||||||
route = model
|
route = model
|
||||||
route = route.to_route if route.respond_to?(:to_route)
|
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
|
end
|
||||||
|
|
||||||
def link_to_model_action(model, action = :show, options = {})
|
def link_to_model_action(model, action = :show, options = {})
|
||||||
|
@ -47,10 +47,16 @@ module SemanticRailsViewHelpers
|
||||||
route = model
|
route = model
|
||||||
route = route.to_route if route.respond_to?(:to_route)
|
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
|
end
|
||||||
|
|
||||||
def li_for(object, options = {}, &block)
|
def li_for(object, options = {}, &block)
|
||||||
|
content_tag(:li, capture(&block), options.merge(semantic_model_data(object)))
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def semantic_model_data(object)
|
||||||
|
SemanticRailsViewHelpers.with_semantic_data do
|
||||||
type = begin
|
type = begin
|
||||||
if object.respond_to?(:model)
|
if object.respond_to?(:model)
|
||||||
object.model.class
|
object.model.class
|
||||||
|
@ -59,7 +65,20 @@ module SemanticRailsViewHelpers
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
content_tag(:li, capture(&block), options.merge('data-type' => type, 'data-id' => object.id))
|
{ '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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue