From 58b42a7392e95505950c4017a2ced4015ac69b83 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 21 Nov 2012 16:31:45 -0500 Subject: [PATCH] ok this kind of works --- .../attributes_builder.rb | 4 ++ lib/semantic_rails_view_helpers/capybara.rb | 4 ++ .../view_helpers.rb | 39 +++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/lib/semantic_rails_view_helpers/attributes_builder.rb b/lib/semantic_rails_view_helpers/attributes_builder.rb index 0e97f46..991096a 100644 --- a/lib/semantic_rails_view_helpers/attributes_builder.rb +++ b/lib/semantic_rails_view_helpers/attributes_builder.rb @@ -1,3 +1,5 @@ +require 'delegate' + module SemanticRailsViewHelpers class AttributesBuilder < SimpleDelegator attr_reader :object @@ -21,6 +23,8 @@ module SemanticRailsViewHelpers value = @object.send(field) end + value = value.to_label if value.respond_to?(:to_label) + value = @context.content_tag(:data, value, 'data-field' => field) if options[:as] diff --git a/lib/semantic_rails_view_helpers/capybara.rb b/lib/semantic_rails_view_helpers/capybara.rb index e69de29..c5cfa36 100644 --- a/lib/semantic_rails_view_helpers/capybara.rb +++ b/lib/semantic_rails_view_helpers/capybara.rb @@ -0,0 +1,4 @@ +def find_attribute(name) + find("[data-field='#{name}']") +end + diff --git a/lib/semantic_rails_view_helpers/view_helpers.rb b/lib/semantic_rails_view_helpers/view_helpers.rb index 2ffbc90..aa0855a 100644 --- a/lib/semantic_rails_view_helpers/view_helpers.rb +++ b/lib/semantic_rails_view_helpers/view_helpers.rb @@ -1,8 +1,47 @@ +require 'semantic_rails_view_helpers/attributes_builder' + module SemanticRailsViewHelpers module ViewHelpers def attributes_for(object, &block) AttributesBuilder.new(object, self, block) end + + def link_to_route(route, *args) + link_to t(".#{route}"), send("#{route}_path", *args), 'data-link' => route + end + + def link_to_collection(route) + collection = route.last + + link_to t(".#{collection}"), polymorphic_url(route), 'data-link' => 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' + end + + def link_to_model_action(model, action = :show, options = {}) + target_action = action + + if action == :destroy + options = options.merge(:method => :delete, 'data-skip-pjax' => 'true') + action = nil + end + + options = Hash[options.collect { |k, v| [ k, CGI.escapeHTML(v.to_s) ] }] + + route = model + route = route.to_route if route.respond_to?(:to_route) + + link_to t(".#{action}"), polymorphic_url(route, :action => action), options.merge("data-action" => target_action) + end + + def li_for(object, &block) + content_tag(:li, capture(&block), 'data-id' => object.id) + end end end