Skip to content

Commit

Permalink
Improve handling and error messages for manually required overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
BDQ committed Feb 7, 2012
1 parent d8b3f3b commit 816b5b9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
1 change: 0 additions & 1 deletion lib/deface.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require "action_view"
require "action_controller"
require "deface/action_view_extensions"
require "deface/template_helper"
require "deface/override"
require "deface/parser"
Expand Down
11 changes: 8 additions & 3 deletions lib/deface/override.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,14 @@ class Override
# * <tt>:attributes</tt> - A hash containing all the attributes to be set on the matched elements, eg: :attributes => {:class => "green", :title => "some string"}
#
def initialize(args, &content)
unless Rails.application.try(:config).respond_to?(:deface) and Rails.application.try(:config).deface.try(:overrides)
@@_early << args
warn "[WARNING] Deface railtie has not initialized yet, override '#{args[:name]}' is being declared too early."
if Rails.application.try(:config).try(:deface).try(:enabled)
unless Rails.application.config.deface.try(:overrides)
@@_early << args
warn "[WARNING] You no longer need to manually require overrides, remove require for '#{args[:name]}'."
return
end
else
warn "[WARNING] You no longer need to manually require overrides, remove require for '#{args[:name]}'."
return
end

Expand Down
30 changes: 17 additions & 13 deletions lib/deface/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,26 @@ def self.activate
# overrides if deface is enabled.
#
initializer "deface.environment", :after => :load_environment_config do |app|
next unless app.config.deface.enabled
if app.config.deface.enabled
# only decorate ActionView if deface is enabled
require "deface/action_view_extensions"

#setup real env object
app.config.deface = Deface::Environment.new
# setup real env object
app.config.deface = Deface::Environment.new

#checks if haml is loaded and enables support
if defined?(Haml)
app.config.deface.haml_support = true
require 'deface/haml_converter'
end

#catchs any overrides that we required manually
app.config.deface.overrides.early_check
# checks if haml is loaded and enables support
if defined?(Haml)
app.config.deface.haml_support = true
require 'deface/haml_converter'
end

if Dir.glob(app.root.join("app/compiled_views", "**/*.erb")).present?
puts "[WARNING] Precompiled views present and Deface is enabled, this can result in overrides being applied twice."
# catchs any overrides that we required manually
app.config.deface.overrides.early_check
else
# deface is disabled so clear any overrides
# that might have been manually required
# won't get loaded but just in case
Deface::Override._early.clear
end
end

Expand Down
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
require 'action_view'
require 'action_controller'
require 'deface'
#have to manually require following two for testing purposes
#have to manually require following three for testing purposes
require 'deface/action_view_extensions'
require 'haml'
require 'deface/haml_converter'

Expand Down

0 comments on commit 816b5b9

Please sign in to comment.