From 6523f9587f29089304985a214e5018769405dd5e Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sun, 24 Feb 2013 17:51:24 -0500 Subject: [PATCH 1/4] better decorator detection --- lib/semantic_rails_view_helpers/view_helpers.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/semantic_rails_view_helpers/view_helpers.rb b/lib/semantic_rails_view_helpers/view_helpers.rb index 327acdd..7af81af 100644 --- a/lib/semantic_rails_view_helpers/view_helpers.rb +++ b/lib/semantic_rails_view_helpers/view_helpers.rb @@ -65,8 +65,8 @@ module SemanticRailsViewHelpers def semantic_model_data(object) SemanticRailsViewHelpers.with_semantic_data do type = begin - if object.respond_to?(:model) - object.model.class + if object.respond_to?(:source) and object.respond_to?(:to_source) + object.source.class else object.class end From ed66c74344368b47753a9ec31a2d2853f94cb76f Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 25 Feb 2013 10:32:59 -0500 Subject: [PATCH 2/4] add basic activeadmin support --- README.md | 5 ++++ .../active_admin.rb | 30 +++++++++++++++++++ .../attributes_table_builder.rb | 2 +- .../view_helpers.rb | 4 +++ 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 lib/semantic_rails_view_helpers/active_admin.rb diff --git a/README.md b/README.md index 0094478..0ec88ed 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,11 @@ You can even make simple tables, a la Active Admin: = f.field :description ``` +### Active Admin + +You can add semantic data to Active Admin's `attributes_table`s in `show` views. Just `require 'semantic_rails_view_helpers/active_admin'` +in an initializer and you can then target attributes in `show` views. + ## Not Finding Things Sometimes the absence of a thing is just as important as the presence of a thing. Make it easy on yourself: diff --git a/lib/semantic_rails_view_helpers/active_admin.rb b/lib/semantic_rails_view_helpers/active_admin.rb new file mode 100644 index 0000000..5575f46 --- /dev/null +++ b/lib/semantic_rails_view_helpers/active_admin.rb @@ -0,0 +1,30 @@ +require 'active_admin/views/components/attributes_table' + +class ActiveAdmin::Views::AttributesTable + def row(attr, &block) + @table << tr do + th do + header_content_for(attr) + end + td do + content_for(attr, block) + end + end + end + + protected + def content_for(attr, block) + value = begin + if block + block.call(@record) + else + content_for_attribute(attr) + end + end + + value = pretty_format(value) + value == "" || value.nil? ? empty_value : value + + %{#{value}}.html_safe + end +end diff --git a/lib/semantic_rails_view_helpers/attributes_table_builder.rb b/lib/semantic_rails_view_helpers/attributes_table_builder.rb index 790942f..e8328ca 100644 --- a/lib/semantic_rails_view_helpers/attributes_table_builder.rb +++ b/lib/semantic_rails_view_helpers/attributes_table_builder.rb @@ -10,7 +10,7 @@ module SemanticRailsViewHelpers def to_s @options[:class] ||= '' - @options[:class] << 'attributes table' + @options[:class] << ' attributes table' @context.content_tag(:table, @options) do @context.capture(self, &@block) diff --git a/lib/semantic_rails_view_helpers/view_helpers.rb b/lib/semantic_rails_view_helpers/view_helpers.rb index 7af81af..f18654b 100644 --- a/lib/semantic_rails_view_helpers/view_helpers.rb +++ b/lib/semantic_rails_view_helpers/view_helpers.rb @@ -7,10 +7,14 @@ module SemanticRailsViewHelpers AttributesBuilder.new(object, self, block) end + alias :semantic_attributes_for :attributes_for + def attributes_table_for(object, options = {}, &block) AttributesTableBuilder.new(object, options, self, block) end + alias :semantic_attributes_table_for :attributes_table_for + def link_to_route(route, *args) options = {} if args.last.kind_of?(::Hash) From d2fac8e685b3de6e81908bb708846d57b9910b23 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 25 Feb 2013 10:47:50 -0500 Subject: [PATCH 3/4] a little cleaner --- .../active_admin.rb | 20 ++----------------- 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/lib/semantic_rails_view_helpers/active_admin.rb b/lib/semantic_rails_view_helpers/active_admin.rb index 5575f46..4362179 100644 --- a/lib/semantic_rails_view_helpers/active_admin.rb +++ b/lib/semantic_rails_view_helpers/active_admin.rb @@ -6,25 +6,9 @@ class ActiveAdmin::Views::AttributesTable th do header_content_for(attr) end - td do - content_for(attr, block) + td 'data-field' => attr do + content_for(block || attr) end end end - - protected - def content_for(attr, block) - value = begin - if block - block.call(@record) - else - content_for_attribute(attr) - end - end - - value = pretty_format(value) - value == "" || value.nil? ? empty_value : value - - %{#{value}}.html_safe - end end From b9e14eac0c2a617a04c0e82e9b0527e03567c06c Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 25 Feb 2013 13:50:25 -0500 Subject: [PATCH 4/4] extend dont_find to Capybara::Element --- README.md | 4 +++ lib/semantic_rails_view_helpers/capybara.rb | 40 ++++++++++++--------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 0ec88ed..f722d02 100644 --- a/README.md +++ b/README.md @@ -89,5 +89,9 @@ Sometimes the absence of a thing is just as important as the presence of a thing # selector's not there dont_find('#user') + +# ...after you've already found something... + +find('.node').dont_find('.something_inside') ``` diff --git a/lib/semantic_rails_view_helpers/capybara.rb b/lib/semantic_rails_view_helpers/capybara.rb index 93fbad3..cf1f301 100644 --- a/lib/semantic_rails_view_helpers/capybara.rb +++ b/lib/semantic_rails_view_helpers/capybara.rb @@ -62,6 +62,26 @@ def find_object_action(object, action) find("[data-type='#{object}'][data-action='#{action}']") end +module DontFindable + def dont_find_wrap(search) + yield + + sleep Capybara.default_wait_time + + yield + + raise Capybara::ElementFound.new(search) + rescue Capybara::ElementNotFound + true + end + + def dont_find(search) + dont_find_wrap(search) do + find(search) + end + end +end + module Capybara class ElementFound < StandardError def initialize(search) @@ -72,26 +92,14 @@ module Capybara @search end end -end -def dont_find_wrap(search) - yield - - sleep Capybara.default_wait_time - - yield - - raise Capybara::ElementFound.new(search) -rescue Capybara::ElementNotFound - true -end - -def dont_find(search) - dont_find_wrap(search) do - find(search) + class Node::Element + include DontFindable end end +include DontFindable + def dont_find_object(object) case object when Class