From b23ad87ad8064d92eca24c9c7f0aab41dadeae16 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 29 Aug 2012 14:28:15 -0400 Subject: [PATCH] allow for fuzzy search, for use with, say, activeadmin --- README.md | 19 +++++++++ app/assets/javascripts/bullseye.js | 22 ---------- app/assets/javascripts/bullseye.js.erb | 54 +++++++++++++++++++++++++ lib/bullseye.rb | 25 ++++++++++++ lib/bullseye/sass/bullseye_functions.rb | 2 +- lib/bullseye/version.rb | 2 +- 6 files changed, 100 insertions(+), 24 deletions(-) delete mode 100644 app/assets/javascripts/bullseye.js create mode 100644 app/assets/javascripts/bullseye.js.erb diff --git a/README.md b/README.md index 94857bc..eeea589 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ becomes `admin/users`). Then, in `application.js`: ``` javascript +//= require jquery //= require bullseye ``` @@ -61,6 +62,24 @@ Want to target that page in your Sass? Use a little string interpolation and a f Piece of cake. +### Fuzzy search + +Plugging Bullseye into an existing app may require making Bullseye work a little harder to target pages. +For instance, you can use Bullseye with ActiveAdmin to target particular actions on models. +However, you can't really add your own `body` tag to their templates. Luckily, they do put in both +the action and controller names in the `class` attribute of the `body` tag. + +Create an initializer, like `config/initializers/bullseye.rb` and add the following: + +``` ruby +Bullseye.configure do |config| + config.fuzzy_search = %{$('body').get(0).classNames.split(/\\s+/)} + config.css_selector = 'body.:action.:controller' +end +``` + +Then you can make your `controller/action.bullseye` files and everything should just work. + ## Hacking _You'll need to install [Penchant](http://github.com/johnbintz/penchant) to mess with the Gemfile diff --git a/app/assets/javascripts/bullseye.js b/app/assets/javascripts/bullseye.js deleted file mode 100644 index ef9456a..0000000 --- a/app/assets/javascripts/bullseye.js +++ /dev/null @@ -1,22 +0,0 @@ -#= require jquery - -this.Bullseye = { - target: function(controller, action, callback) { - this.targets[controller] = (this.targets[controller] || {}); - this.targets[controller][action] = callback; - }, - - targets: {}, - - exec: function(controller, action) { - if (this.targets[controller] && this.targets[controller][action]) { - this.targets[controller][action].apply(this.context); - } - } -}; - -Bullseye.context = this; - -$(function() { - Bullseye.exec($('body').data('controller'), $('body').data('action')); -}); diff --git a/app/assets/javascripts/bullseye.js.erb b/app/assets/javascripts/bullseye.js.erb new file mode 100644 index 0000000..00e8439 --- /dev/null +++ b/app/assets/javascripts/bullseye.js.erb @@ -0,0 +1,54 @@ +this.Bullseye = { + target: function(controller, action, callback) { + this.targets[controller] = (this.targets[controller] || {}); + this.targets[controller][action] = callback; + }, + + targets: {}, + + exec: function() { + var controller, action; + + switch (arguments.length) { + case 2: + controller = arguments[0]; + action = arguments[1]; + break; + + case 1: + choices = arguments[0]; + + choice_loop: + for (controller_i = 0; controller_i < choices.length; ++controller_i) { + controller = choices[controller_i]; + if (this.targets[controller]) { + for (action_i = 0; action_i < choices.length; ++action_i) { + action = choices[action_i]; + + if (this.targets[controller][action]) { + break choice_loop; + } + } + } + } + break; + } + + if (this.targets[controller] && this.targets[controller][action]) { + this.targets[controller][action].apply(this.context); + } + } +}; + +Bullseye.context = this; + +$(function() { + Bullseye.exec( + <% if Bullseye.config.fuzzy_search %> + <%= Bullseye.config.fuzzy_search %> + <% else %> + <%= Bullseye.config.js_controller_search %>, + <%= Bullseye.config.js_action_search %> + <% end %> + ); +}); diff --git a/lib/bullseye.rb b/lib/bullseye.rb index 8d43773..ec454c8 100644 --- a/lib/bullseye.rb +++ b/lib/bullseye.rb @@ -2,3 +2,28 @@ require "bullseye/version" require 'bullseye/engine' if defined?(Rails::Engine) require 'bullseye/tilt/bullseye_template' require 'bullseye/sass/bullseye_functions' + +module Bullseye + class Configuration + attr_accessor :js_controller_search, :js_action_search, :css_selector, :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']} + + @fuzzy_search = false + end + end + + class << self + def config + @config ||= Configuration.new + end + + def configure + block_given? ? yield(config) : config + end + end +end diff --git a/lib/bullseye/sass/bullseye_functions.rb b/lib/bullseye/sass/bullseye_functions.rb index 609f863..4b28bc2 100644 --- a/lib/bullseye/sass/bullseye_functions.rb +++ b/lib/bullseye/sass/bullseye_functions.rb @@ -8,7 +8,7 @@ module Sass::Script::Functions action = parts.pop controller = parts.join('/') - Sass::Script::String.new("body[data-action='#{action}'][data-controller='#{controller}']") + Sass::Script::String.new(Bullseye.config.css_selector.gsub(':action', action).gsub(':controller', controller)) end end diff --git a/lib/bullseye/version.rb b/lib/bullseye/version.rb index d0aabb6..d7d8330 100644 --- a/lib/bullseye/version.rb +++ b/lib/bullseye/version.rb @@ -1,3 +1,3 @@ module Bullseye - VERSION = "0.0.3" + VERSION = "0.0.4" end