From 051766915f92b725b2d99094991733eb75b283a1 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 19 Sep 2012 11:32:56 -0400 Subject: [PATCH] rearrange stuff and make some other things super-cool --- README.md | 19 ++++++++++++++- lib/bullseye.rb | 3 ++- lib/bullseye/engine.rb | 2 ++ lib/bullseye/find_parts.rb | 24 +++++++++++++++++++ .../helpers/bullseye_controller_helper.rb | 14 +++++++++++ lib/bullseye/helpers/bullseye_helper.rb | 15 +++++++++++- lib/bullseye/sass/bullseye_functions.rb | 11 +++++---- lib/bullseye/tilt/bullseye_template.rb | 6 ++--- lib/bullseye/tilt/find_parts.rb | 18 -------------- lib/bullseye/version.rb | 2 +- 10 files changed, 85 insertions(+), 29 deletions(-) create mode 100644 lib/bullseye/find_parts.rb create mode 100644 lib/bullseye/helpers/bullseye_controller_helper.rb delete mode 100644 lib/bullseye/tilt/find_parts.rb diff --git a/README.md b/README.md index f11e24e..72f57da 100644 --- a/README.md +++ b/README.md @@ -83,13 +83,30 @@ Create an initializer, like `config/initializers/bullseye.rb` and add the follow Bullseye.configure do |config| config.fuzzy_search = %{$('body').get(0).classNames.split(/\\s+/)} config.css_selector = 'body.:action.:controller' + config.html_tag = { 'class' => ':action :controller' } end ``` Then you can make your `controller/action.bullseye` files and everything should just work. +## Force the name of the controller/action + +For things like error handling in `rescue_from`, you can force the `action` and `controller` that +Bullseye will use: + +``` ruby +rescue_from StandardError do |e| + bullseye_target 'application/errors/http_500' + + render 'http_500' +end +``` + +This will resolve to the controller `application/errors` and the action `http_500` in the Bullseye +HTML `body` tag for that page. + ## Hacking _You'll need to install [Penchant](http://github.com/johnbintz/penchant) to mess with the Gemfile -during development._ +during development. diff --git a/lib/bullseye.rb b/lib/bullseye.rb index ec454c8..9b5f541 100644 --- a/lib/bullseye.rb +++ b/lib/bullseye.rb @@ -5,13 +5,14 @@ require 'bullseye/sass/bullseye_functions' module Bullseye class Configuration - attr_accessor :js_controller_search, :js_action_search, :css_selector, :fuzzy_search + attr_accessor :js_controller_search, :js_action_search, :css_selector, :html_tag, :fuzzy_search def initialize @js_controller_search = %{$('body').data('controller')} @js_action_search = %{$('body').data('action')} @css_selector = %{body[data-action=':action'][data-controller=':controller']} + @html_tag = { 'data-action' => ':action', 'data-controller' => ':controller' } @fuzzy_search = false end diff --git a/lib/bullseye/engine.rb b/lib/bullseye/engine.rb index 654f063..b1dad85 100644 --- a/lib/bullseye/engine.rb +++ b/lib/bullseye/engine.rb @@ -1,9 +1,11 @@ require 'bullseye/helpers/bullseye_helper' +require 'bullseye/helpers/bullseye_controller_helper' module Bullseye class Engine < ::Rails::Engine initializer 'bullseye.view_helpers' do ActionView::Base.send(:include, Bullseye::Helpers::BullseyeHelper) + ActionController::Base.send(:include, Bullseye::Helpers::BullseyeControllerHelper) end initializer 'bullseye.sprockets', :after => 'sprockets.environment' do |app| diff --git a/lib/bullseye/find_parts.rb b/lib/bullseye/find_parts.rb new file mode 100644 index 0000000..e91e457 --- /dev/null +++ b/lib/bullseye/find_parts.rb @@ -0,0 +1,24 @@ +module Bullseye + module FindParts + def actions + parts.last.split('-') + end + + def controller + parts[0..-2].join('-') + end + + def parts + @source.split('/') + end + + class PartFinder + include Bullseye::FindParts + + def initialize(source) + @source = source + end + end + end +end + diff --git a/lib/bullseye/helpers/bullseye_controller_helper.rb b/lib/bullseye/helpers/bullseye_controller_helper.rb new file mode 100644 index 0000000..662ea98 --- /dev/null +++ b/lib/bullseye/helpers/bullseye_controller_helper.rb @@ -0,0 +1,14 @@ +require 'bullseye/find_parts' + +module Bullseye + module Helpers + module BullseyeControllerHelper + def bullseye_target(target) + part_finder = Bullseye::FindParts::PartFinder.new(target) + + @__bullseye_action = part_finder.actions.first + @__bullseye_controller = part_finder.controller + end + end + end +end diff --git a/lib/bullseye/helpers/bullseye_helper.rb b/lib/bullseye/helpers/bullseye_helper.rb index d51fa80..464921f 100644 --- a/lib/bullseye/helpers/bullseye_helper.rb +++ b/lib/bullseye/helpers/bullseye_helper.rb @@ -2,7 +2,20 @@ module Bullseye module Helpers module BullseyeHelper def bullseye_body(options = {}, &block) - content_tag(:body, capture(&block), { 'data-action' => action_name, 'data-controller' => controller_path }.merge(options)).html_safe + attributes = Bullseye.config.html_tag.collect do |key, value| + [ key, value.gsub(':action', __bullseye_action).gsub(':controller', __bullseye_controller) ] + end + + content_tag(:body, capture(&block), Hash[attributes].merge(options)).html_safe + end + + private + def __bullseye_action + @__bullseye_action || action_name + end + + def __bullseye_controller + @__bullseye_controller || controller_path end end end diff --git a/lib/bullseye/sass/bullseye_functions.rb b/lib/bullseye/sass/bullseye_functions.rb index 4b28bc2..c8914b7 100644 --- a/lib/bullseye/sass/bullseye_functions.rb +++ b/lib/bullseye/sass/bullseye_functions.rb @@ -1,14 +1,17 @@ require 'sass' +require 'bullseye/find_parts' module Sass::Script::Functions def bullseye(target) assert_type target, :String - parts = target.value.split('/') - action = parts.pop - controller = parts.join('/') + part_finder = Bullseye::FindParts::PartFinder.new(target.value) - Sass::Script::String.new(Bullseye.config.css_selector.gsub(':action', action).gsub(':controller', controller)) + selectors = part_finder.actions.collect do |action| + Bullseye.config.css_selector.gsub(':action', action).gsub(':controller', part_finder.controller) + end + + Sass::Script::String.new(selectors.join(',')) end end diff --git a/lib/bullseye/tilt/bullseye_template.rb b/lib/bullseye/tilt/bullseye_template.rb index 53a31bb..37a4f46 100644 --- a/lib/bullseye/tilt/bullseye_template.rb +++ b/lib/bullseye/tilt/bullseye_template.rb @@ -1,11 +1,11 @@ require 'tilt' -require 'bullseye/tilt/find_parts' +require 'bullseye/find_parts' require 'json' module Bullseye module Tilt class BullseyeTemplate < ::Tilt::Template - include Bullseye::Tilt::FindParts + include Bullseye::FindParts def self.default_mime_type 'application/javascript' @@ -15,7 +15,7 @@ module Bullseye end def evaluate(scope, locals, &block) - @scope = scope + @source = scope.logical_path[1..-1] <<-JS Bullseye.target('#{controller}', #{actions.to_json}, function() { diff --git a/lib/bullseye/tilt/find_parts.rb b/lib/bullseye/tilt/find_parts.rb deleted file mode 100644 index 065c763..0000000 --- a/lib/bullseye/tilt/find_parts.rb +++ /dev/null @@ -1,18 +0,0 @@ -module Bullseye - module Tilt - module FindParts - def actions - parts.last.split('-') - end - - def controller - parts[0..-2].join('/') - end - - def parts - @scope.logical_path.split('/')[1..-1] - end - end - end -end - diff --git a/lib/bullseye/version.rb b/lib/bullseye/version.rb index 1a46414..04fba77 100644 --- a/lib/bullseye/version.rb +++ b/lib/bullseye/version.rb @@ -1,3 +1,3 @@ module Bullseye - VERSION = "0.0.5" + VERSION = "0.0.6" end