rearrange stuff and make some other things super-cool

This commit is contained in:
John Bintz 2012-09-19 11:32:56 -04:00
parent cf004ac1d8
commit 051766915f
10 changed files with 85 additions and 29 deletions

View File

@ -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.

View File

@ -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

View File

@ -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|

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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() {

View File

@ -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

View File

@ -1,3 +1,3 @@
module Bullseye
VERSION = "0.0.5"
VERSION = "0.0.6"
end