From 6ae523a21dfa47a721dad1f65ba21d01028487cd Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 8 Dec 2012 09:55:53 -0500 Subject: [PATCH] enable turning on/off semantic data helpers --- lib/semantic_rails_view_helpers.rb | 11 +++++ .../attributes_builder_base.rb | 5 +- .../attributes_table_builder.rb | 11 ++++- lib/semantic_rails_view_helpers/engine.rb | 4 +- .../view_helpers.rb | 47 +++++++++++++------ 5 files changed, 61 insertions(+), 17 deletions(-) diff --git a/lib/semantic_rails_view_helpers.rb b/lib/semantic_rails_view_helpers.rb index 8f7de28..3f6cccf 100644 --- a/lib/semantic_rails_view_helpers.rb +++ b/lib/semantic_rails_view_helpers.rb @@ -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 diff --git a/lib/semantic_rails_view_helpers/attributes_builder_base.rb b/lib/semantic_rails_view_helpers/attributes_builder_base.rb index 8be9bab..a469bfb 100644 --- a/lib/semantic_rails_view_helpers/attributes_builder_base.rb +++ b/lib/semantic_rails_view_helpers/attributes_builder_base.rb @@ -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 }) diff --git a/lib/semantic_rails_view_helpers/attributes_table_builder.rb b/lib/semantic_rails_view_helpers/attributes_table_builder.rb index fde8b98..790942f 100644 --- a/lib/semantic_rails_view_helpers/attributes_table_builder.rb +++ b/lib/semantic_rails_view_helpers/attributes_table_builder.rb @@ -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 diff --git a/lib/semantic_rails_view_helpers/engine.rb b/lib/semantic_rails_view_helpers/engine.rb index 7ac73e6..a789d2f 100644 --- a/lib/semantic_rails_view_helpers/engine.rb +++ b/lib/semantic_rails_view_helpers/engine.rb @@ -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 diff --git a/lib/semantic_rails_view_helpers/view_helpers.rb b/lib/semantic_rails_view_helpers/view_helpers.rb index 46fac62..51ddf97 100644 --- a/lib/semantic_rails_view_helpers/view_helpers.rb +++ b/lib/semantic_rails_view_helpers/view_helpers.rb @@ -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