Skip to content

Commit

Permalink
Add functionality to raise error or not depending on the current envi…
Browse files Browse the repository at this point in the history
…ronment.
  • Loading branch information
danhper committed Jul 27, 2014
1 parent 8672e35 commit 3533f70
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
cdn_tags (0.4.1)
cdn_tags (0.5.0)
rails (>= 3.0)

GEM
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ This will automatically add the files to `Rails.application.config.assets.precom
* `scripts_urls` (`Hash`, default: `{}`): The scripts that should be served from a CDN.
* `stylesheets_urls` (`Hash`, default: `{}`): The stylesheets that should be served from a CDN.
* `add_to_precompile` (`true`|`false`, default: `true`): Automatically add assets to `Rails.application.config.assets` or not.
* `raise_on_missing` (`true`|`false`, default: `false`): Raise an exception or not if the asset used with CDN helper is not defined in `scripts_urls` or `stylesheets_urls`.
* `raise_on_missing` (`true`|`false`|`Array`, default: `false`): Raise an exception or not if the asset used with CDN helper is not defined in `scripts_urls` or `stylesheets_urls`. If an array is given, an exception will be set if the current environment is in this array.
* `cdn_environments` (`Array`, default: `[:production]`): The environments in which CDN should be used instead of normal asset.

## Usage
Expand Down
20 changes: 16 additions & 4 deletions lib/cdn_tags/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module CdnTags
class Configuration
attr_accessor :scripts_urls, :stylesheets_urls, :environment
attr_accessor :raise_on_missing, :add_to_precompile, :cdn_environments
attr_reader :should_raise

def initialize
self.scripts_urls = {}
Expand All @@ -15,12 +16,24 @@ def initialize
end

def post_configure_hooks
self.update_rails_precompile!
self.fix_cdn_environments_keys!
update_rails_precompile!
fix_keys!
set_should_raise!
end

def fix_cdn_environments_keys!
private
def set_should_raise!
if self.raise_on_missing == !!self.raise_on_missing
@should_raise = self.raise_on_missing
else
self.raise_on_missing.map! { |s| s.to_sym }
@should_raise = self.raise_on_missing.include? self.environment
end
end

def fix_keys!
self.cdn_environments.map! { |s| s.to_sym }
self.environment = self.environment.to_sym
end

def update_rails_precompile!
Expand All @@ -31,7 +44,6 @@ def update_rails_precompile!
Rails.application.config.assets.precompile += added_precompile
end

private
def should_append_extension(filename)
extname = File.extname(filename)
extname.empty? or /[0-9]+/.match extname[1..-1]
Expand Down
10 changes: 7 additions & 3 deletions lib/cdn_tags/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ def stylesheet_cdn_link_tag(*sources)

def self.map_sources(sources, config_key)
config = CdnTags.configuration
return sources unless config.cdn_environments.include? config.environment.to_sym
do_replace = config.cdn_environments.include? config.environment.to_sym
return sources unless do_replace || config.should_raise
sources.map do |s|
src = config.send(config_key)[s]
if src.nil?
raise CdnTags::Error.new(config), "#{s} is not defined. Check CdnTags configuration." if config.raise_on_missing
raise CdnTags::Error.new(config), "#{s} is not defined. Check CdnTags configuration." if config.should_raise
s
else
src
do_replace ? src : s
end
end
end

def maybe_raise(src, config_key)
end
end
2 changes: 1 addition & 1 deletion lib/cdn_tags/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module CdnTags
VERSION = "0.4.1"
VERSION = "0.5.0"
end
5 changes: 4 additions & 1 deletion lib/generators/cdn_tags/install/templates/cdn_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
}

# Set to true to raise an exception if the demanded
# asset is not defined above
# asset is not defined above. The following values are possible.
# true: will always raise when not found
# false: will never raise
# Array: will raise if the current environment is in this array
# c.raise_on_missing = false

# Set to false to avoid adding the above assets
Expand Down
45 changes: 42 additions & 3 deletions spec/cdn_tags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@ def link_href(html)
end
expect(Rails.application.config.assets.precompile).not_to include('angular.js')
end

describe '#should_raise' do
it 'should work with booleans' do
CdnTags.configure { |c| c.raise_on_missing = false }
expect(CdnTags.configuration.should_raise).to be false
CdnTags.configure { |c| c.raise_on_missing = true }
expect(CdnTags.configuration.should_raise).to be true
end

it 'should work with arrays' do
CdnTags.configure do |c|
c.environment = :development
c.raise_on_missing = [:development, :test]
end
expect(CdnTags.configuration.should_raise).to be true
CdnTags.configure do |c|
c.environment = :production
end
expect(CdnTags.configuration.should_raise).to be false
end
end
end

describe CdnTags::Helpers do
Expand Down Expand Up @@ -91,6 +112,16 @@ def link_href(html)
expect(send(t[:extracter], tag)).to eq(expected)
end

it 'should not replace in dev when configured to raise' do
CdnTags.configure do |c|
c.raise_on_missing = [:development]
c.environment = "development"
end
tag = view.send(t[:method], t[:asset])
expected = view.send(t[:path_helper], t[:asset])
expect(send(t[:extracter], tag)).to eq(expected)
end

it 'should replace sources in production' do
CdnTags.configuration.environment = "production"
tag = view.send(t[:method], t[:asset])
Expand All @@ -102,8 +133,8 @@ def link_href(html)
it 'should replace sources in added environments' do
CdnTags.configure do |c|
c.cdn_environments << "staging"
c.environment = "staging"
end
CdnTags.configuration.environment = "staging"
tag = view.send(t[:method], t[:asset])
urls = CdnTags.configuration.send(t[:config_key])
expected = urls[t[:asset]]
Expand All @@ -117,11 +148,19 @@ def link_href(html)
expect(send(t[:extracter], tag)).to eq(expected)
end

it 'should raise error when configured' do
it 'should raise error when raise_on_missing is true' do
CdnTags.configure do |c|
c.raise_on_missing = true
c.environment = "production"
end
expect { view.send(t[:method], "foo") }.to raise_error(CdnTags::Error)
end

it 'should raise error when env is in raise_on_missing' do
CdnTags.configure do |c|
c.raise_on_missing = [:development, :test]
c.environment = "development"
end
CdnTags.configuration.environment = "production"
expect { view.send(t[:method], "foo") }.to raise_error(CdnTags::Error)
end
end
Expand Down

0 comments on commit 3533f70

Please sign in to comment.