Browse files

Add support for CONFIG.theme, which can name a theme framework to load

  • Loading branch information...
1 parent 6cefc5e commit 4b64dd2adda67d6f567bfa6f83d8b360a1c9391d Charles Jolley committed Jan 25, 2009
View
6 Buildfile
@@ -52,7 +52,11 @@ mode :all do
:minify_css => false,
# use the default layout defined in SproutCore
- :layout => 'sproutcore:lib/index.rhtml'
+ :layout => 'sproutcore:lib/index.rhtml',
+
+ # name a framework to use as the theme. will be included in required
+ # frameworks automatically if found.
+ :theme => 'sproutcore/standard_theme'
end
View
1 lib/sproutcore/builders/html.rb
@@ -79,6 +79,7 @@ def initialize(entry)
# config settings.
def expand_required_targets(target, opts = {})
opts[:debug] = target.config.load_debug
+ opts[:theme] = true
return target.expand_required_targets(opts)
end
View
19 lib/sproutcore/helpers/static_helper.rb
@@ -144,6 +144,24 @@ def loc(string, opts = {})
return strings_hash(language)[string] || string
end
+ # Returns the CSS class name dictated by the current theme. You can
+ # also pass an optional default value to use if no theme is specified
+ # in the config. The value returned here will use either the
+ # theme_name set in the target's config or the theme_name set by the
+ # theme framework, if set.
+ def theme_name(opts ={})
+ ret = opts[:default] || 'sc-theme'
+ if target.config.theme_name
+ ret = target.config.theme_name
+ elsif target.config.theme
+ if theme_target = target.target_for(target.config.theme)
+ ret = theme_target.config.theme_name || ret
+ end
+ end
+ return ret
+ end
+
+
private
# Returns a merged strings hash from all of the required bundles. Used
@@ -211,6 +229,7 @@ def combined_entries(t, opts, entry_name, &block)
end
end
+
end
end
View
25 lib/sproutcore/models/target.rb
@@ -127,18 +127,29 @@ def reload_config!
# Array of Targets
#
def required_targets(opts={})
- key = [:debug, :test].map { |k| opts[k] ? k : nil }.compact.join('.')
+
+ # compute cache key for these options
+ key = [:debug, :test, :theme].map do |k|
+ opts[k] ? k : nil
+ end
+ key = key.compact.join('.')
+
+ # Return cache value if found
ret = (@required_targets ||= {})[key]
return ret unless ret.nil?
- # compute
- ret = [config.required || []]
+ # else compute return value, respecting options
+ ret = [config.required]
if opts[:debug] && config.debug_required
ret << config.debug_required
end
if opts[:test] && config.test_required
ret << config.test_required
+ end
+ if opts[:theme] && self.loads_theme? && config.theme
+ ret << config.theme
end
+
ret = ret.flatten.compact.map do |n|
if (t = target_for(n)).nil?
SC.logger.warn "Could not find target #{n} that is required by #{target_name}"
@@ -151,6 +162,14 @@ def required_targets(opts={})
return ret
end
+ # Returns true if this target can load a theme. Default returns true
+ # only if the target_type == :app, but you can override this by setting
+ # the value yourself.
+ def loads_theme?;
+ ret = self[:loads_theme]
+ ret.nil? ? (target_type == :app) : ret
+ end
+
# Returns the expanded list of required targets, ordered as they actually
# need to be loaded.
#
View
1 spec/fixtures/builder_tests/frameworks/sample_theme/Buildfile
@@ -0,0 +1 @@
+config :all, :theme_name => 'sample_theme_name'
View
34 spec/lib/builders/html_spec.rb
@@ -10,6 +10,7 @@
# most of these tests assume load_debug is turned off like it would be
# in production mode
@target.config.load_debug = false
+ @target.config.theme = nil
# run std rules to run manifest. Then verify preconditions to make sure
# no other changes to the build system effect the ability of these tests
@@ -109,6 +110,16 @@
@target.config.load_debug = true
@builder.expand_required_targets(@target).should == @target.expand_required_targets(:debug => true)
end
+
+ it "includes theme if theme is specified and target is app" do
+ @builder = SC::Builder::Html.new(@index_entry)
+ @target.config.theme = :sample_theme
+
+ expected = @project.target_for :sample_theme
+ expected.should_not be_nil #precondition
+ @builder.expand_required_targets(@target).should include(expected)
+ end
+
end
# templates should expect to be able to access certain environmental
@@ -168,6 +179,29 @@
@builder.instance_variable_get('@layout').should == 'bar'
end
+ describe "exposes theme_name()" do
+
+ it "returns passed default value or 'sc-theme' if no theme is configd" do
+ @target.config.theme = nil # precondition
+ @builder.theme_name.should == 'sc-theme'
+ @builder.theme_name(:default => 'foo').should == 'foo'
+ end
+
+ it "returns targets theme_name config if set" do
+ @target.config.theme = 'sample_theme'
+ @target.config.theme_name = "foo"
+ @builder.theme_name.should == 'foo'
+ end
+
+ it "returns theme_name set in theme target if set and not overridden in app itself" do
+ @target.config.theme = 'sample_theme'
+ @target.config.theme_name = nil # precondition
+
+ # this value is set in the sample_theme/Buildfile
+ @builder.theme_name.should == 'sample_theme_name'
+ end
+ end
+
describe "exposes loc()" do
it "maps passed string to local target strings.js, if present" do
View
18 spec/lib/models/target/required_targets_spec.rb
@@ -74,4 +74,22 @@
capture('stderr') { target.required_targets(:debug => true) }.size.should_not == 0
end
+ it "should include any CONFIG.theme if passed :theme => true && target_type == :app" do
+ expected = @project.target_for 'sproutcore/standard_theme'
+
+ target = @project.target_for :contacts
+ target.target_type.should == :app # precondition
+ target.config.theme = 'sproutcore/standard_theme'
+ target.required_targets().should_not include(expected)
+ target.required_targets(:theme => false).should_not include(expected)
+ target.required_targets(:theme => true).should include(expected)
+
+ target = @project.target_for :sproutcore
+ target.target_type.should_not == :app # precondition
+ target.config.theme = 'sproutcore/standard_theme'
+ target.required_targets().should_not include(expected)
+ target.required_targets(:theme => false).should_not include(expected)
+ target.required_targets(:theme => true).should_not include(expected)
+ end
+
end

0 comments on commit 4b64dd2

Please sign in to comment.