Skip to content

Commit

Permalink
Add Configuration specs
Browse files Browse the repository at this point in the history
  • Loading branch information
spohlenz committed Feb 24, 2012
1 parent 8e0a696 commit 4a6ac89
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 24 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -9,6 +9,7 @@ gem 'json', :platforms => [:ruby_18]

group :test do
gem "rspec-rails"
gem "guard-rspec"
end

gemspec
5 changes: 5 additions & 0 deletions Guardfile
@@ -0,0 +1,5 @@
guard 'rspec', :version => 2 do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/tinymce/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
end
1 change: 1 addition & 0 deletions lib/tinymce/rails.rb
Expand Up @@ -2,6 +2,7 @@ module TinyMCE
module Rails
require 'tinymce/rails/engine'
require 'tinymce/rails/version'
require "tinymce/rails/configuration"

def self.base
Engine.config.tinymce.base ||
Expand Down
38 changes: 38 additions & 0 deletions lib/tinymce/rails/configuration.rb
@@ -0,0 +1,38 @@
module TinyMCE::Rails
class Configuration
def self.defaults
{
"mode" => "textareas",
"theme" => "advanced",
"language" => default_language,
"editor_selector" => "tinymce"
}
end

attr_reader :options

def initialize
@options = self.class.defaults
end

# Default language falls back to English if current locale is not available.
def self.default_language
available_languages.include?(I18n.locale.to_s) ? I18n.locale.to_s : "en"
end

# Searches asset paths for TinyMCE language files.
def self.available_languages
assets.paths.map { |path|
files = assets.entries(File.join(path, "tinymce/langs"))
files.map { |file|
asset = assets.attributes_for(File.join(path, file))
asset.logical_path.sub(/\.js$/, "")
}
}.flatten.uniq
end

def self.assets
Rails.application.assets
end
end
end
8 changes: 3 additions & 5 deletions lib/tinymce/rails/engine.rb
@@ -1,7 +1,5 @@
module TinyMCE
module Rails
class Engine < ::Rails::Engine
config.tinymce = ActiveSupport::OrderedOptions.new
end
module TinyMCE::Rails
class Engine < ::Rails::Engine
config.tinymce = ActiveSupport::OrderedOptions.new
end
end
3 changes: 3 additions & 0 deletions spec/assets/tinymce/langs/erb.js.erb
@@ -0,0 +1,3 @@
tinyMCE.addI18n({
erb: <%= {}.to_json %>
});
3 changes: 3 additions & 0 deletions spec/assets/tinymce/langs/pirate.js
@@ -0,0 +1,3 @@
tinyMCE.addI18n({
pirate: {}
});
41 changes: 41 additions & 0 deletions spec/lib/configuration_spec.rb
@@ -0,0 +1,41 @@
require 'spec_helper'

module TinyMCE::Rails
describe Configuration do
it "has default options" do
Configuration.defaults.should eq(
"mode" => "textareas",
"theme" => "advanced",
"language" => "en",
"editor_selector" => "tinymce"
)
end

it "uses the current locale if available" do
I18n.locale = "pirate"
Configuration.defaults["language"].should eq("pirate")
end

it "falls back to English if the current locale is not available" do
I18n.locale = "missing"
Configuration.defaults["language"].should eq("en")
end

it "uses default options when instantiated without a filename" do
config = Configuration.new
config.options.should eq(Configuration.defaults)
end

it "loads configuration from YAML file when instantiated with a filename"

it "detects available languages" do
langs = Configuration.available_languages

langs.should include("en")
langs.should include("pirate")
langs.should include("erb")

langs.should_not include("missing")
end
end
end
40 changes: 22 additions & 18 deletions spec/lib/tinymce_rails_spec.rb
@@ -1,22 +1,26 @@
require 'spec_helper'

describe TinyMCE::Rails do
before(:each) do
::Rails.application.config.action_controller.asset_host = nil
ActionController::Base.config.relative_url_root = nil
end

it "has a base path" do
TinyMCE::Rails.base.should eq("/assets/tinymce")
end

it "uses the asset host in the base path when set" do
::Rails.application.config.action_controller.asset_host = "http://assets.example.com"
TinyMCE::Rails.base.should eq("http://assets.example.com/assets/tinymce")
end

it "uses the relative url root in the base path when set" do
ActionController::Base.config.relative_url_root = "/subfolder"
TinyMCE::Rails.base.should eq("/subfolder/assets/tinymce")
module TinyMCE
describe Rails do
describe ".base" do
before(:each) do
::Rails.application.config.action_controller.asset_host = nil
ActionController::Base.config.relative_url_root = nil
end

it "has a base path" do
TinyMCE::Rails.base.should eq("/assets/tinymce")
end

it "uses the asset host in the base path when set" do
::Rails.application.config.action_controller.asset_host = "http://assets.example.com"
TinyMCE::Rails.base.should eq("http://assets.example.com/assets/tinymce")
end

it "uses the relative url root in the base path when set" do
ActionController::Base.config.relative_url_root = "/subfolder"
TinyMCE::Rails.base.should eq("/subfolder/assets/tinymce")
end
end
end
end
4 changes: 3 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -5,7 +5,9 @@

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
Dir[File.expand_path("../support/**/*.rb", __FILE__)].each {|f| require f}

Rails.application.assets.append_path File.expand_path("../assets", __FILE__)

RSpec.configure do |config|
end

0 comments on commit 4a6ac89

Please sign in to comment.