allow for fuzzy search, for use with, say, activeadmin
This commit is contained in:
parent
ec9eb1ee18
commit
b23ad87ad8
19
README.md
19
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
|
||||
|
@ -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'));
|
||||
});
|
54
app/assets/javascripts/bullseye.js.erb
Normal file
54
app/assets/javascripts/bullseye.js.erb
Normal file
@ -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 %>
|
||||
);
|
||||
});
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -1,3 +1,3 @@
|
||||
module Bullseye
|
||||
VERSION = "0.0.3"
|
||||
VERSION = "0.0.4"
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user