Skip to content

Commit

Permalink
added rails 3 generator for creating localized.yml and the localized …
Browse files Browse the repository at this point in the history
…concerns used within Active Record models
  • Loading branch information
peppyheppy committed Jul 26, 2011
1 parent 5f967a9 commit 63dc12c
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 3 deletions.
8 changes: 7 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
PATH
remote: .
specs:
localized (0.0.4)
localized (0.0.5)
bitfields (>= 0.4.0)
rails (>= 3.0)

GEM
Expand Down Expand Up @@ -37,11 +38,15 @@ GEM
archive-tar-minitar (0.5.2)
arel (1.0.1)
activesupport (~> 3.0.0)
bitfields (0.4.0)
builder (2.1.2)
columnize (0.3.4)
diff-lcs (1.1.2)
erubis (2.6.6)
abstract (>= 1.0.0)
generator_spec (0.8.3)
rails (~> 3.0)
rspec-rails
i18n (0.4.2)
linecache19 (0.5.12)
ruby_core_source (>= 0.1.4)
Expand Down Expand Up @@ -103,6 +108,7 @@ PLATFORMS
ruby

DEPENDENCIES
generator_spec
localized!
rspec (= 2.6.0)
rspec-rails (= 2.6.0)
Expand Down
56 changes: 56 additions & 0 deletions lib/generators/localized/localized_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class LocalizedGenerator < Rails::Generators::Base
desc "This generator copies a sample localized.yml and creates a concerns for
scopes and site flags on active record models."
argument :sites, :type => :array, :default => ['us:en-US', 'uk:en-UK', 'it:it-IT'], :banner => "site:locale us:en-US"

def create_localized_config
# create the
create_file "config/localized.yml", <<-YAML_FILE
# this file was originally generated by the localized generator
default_host_prefix: 'www.'
default_site: 'us'
site_locale_map:
#{generate_yaml_from_sites}
YAML_FILE
end

desc "create the concerns mixin for adding bitfield and default scope support"
def create_concerns_lib
create_file "app/concerns/localized/sites.rb", <<-SITES
# this file was originally generated by the localized generator
module Localized::Sites
extend ActiveSupport::Concern
included do
# create bitfield for all supported sites
include Bitfields
# NOTE: order should not change, if it does site data may become corrupt.
bitfield :sites,
#{generate_flags}
# default scope to filter by current locale to site map
default_scope self.send(Localized::Config.locale_to_site_map[I18n.locale])
end
end
SITES
end

private

def generate_flags
flag_idx = 0
parsed_sites.map(&:first).map do |s|
flag_idx = flag_idx == 0 ? 1 : flag_idx * 2
" #{flag_idx} => :#{s},"
end.join("\n").chop
end

def generate_yaml_from_sites
parsed_sites.map { |(s,l)| " #{s.strip}: '#{l.strip}'"}.join("\n")
end

def parsed_sites
sites.compact.map{|s| [*s.split(':')]}
end

end
4 changes: 2 additions & 2 deletions lib/localized.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ module Localized; end
require 'localized/config'
require 'localized/helper'
require 'localized/convert'
require 'bitfields'

# load the modules into the rails world
[
ActionView::Base,
ActionView::Base,
ActionController::Base
].each { |mod| mod.send :include, Localized::Helper }

2 changes: 2 additions & 0 deletions localized.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ Gem::Specification.new do |s|
s.description = "This gem allows you to set locale using a subdomain and url helper support for switching sites."
s.files = s.files = `git ls-files`.split("\n")
s.add_runtime_dependency "rails", ">= 3.0"
s.add_runtime_dependency "bitfields", ">= 0.4.0"
s.add_development_dependency "ruby-debug19"
s.add_development_dependency "rspec", "2.6.0"
s.add_development_dependency "rspec-rails", "2.6.0"
s.add_development_dependency "generator_spec"
end

74 changes: 74 additions & 0 deletions rspec_watcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
if __FILE__ == $0
puts "Run with: watchr #{__FILE__}. \n\nRequired gems: watchr rev"
exit 1
end

# --------------------------------------------------
# Convenience Methods
# --------------------------------------------------
def run(cmd)
puts(cmd)
system(cmd)
end

def run_all_specs
run "rake -s spec"
end

def run_single_spec *spec
spec = spec.join(' ')
run "rspec -O spec/spec.opts #{spec}"
end

def run_specs_with_shared_examples(shared_example_filename, spec_path = 'spec')

# Returns the names of the shared examples in filename
def shared_examples(filename)
lines = File.readlines(filename)
lines.grep(/shared_examples_for[\s'"]+(.+)['"]\s*[do|\{]/) do |matching_line|
$1
end
end

# Returns array with filenames of the specs using shared_example
def specs_with_shared_example(shared_example, path)
command = "grep -lrE 'it_should_behave_like .(#{shared_example}).' #{path}"
`#{command}`.split
end

shared_examples(shared_example_filename).each do |shared_example|
specs_to_run = specs_with_shared_example(shared_example, spec_path)
run_single_spec(specs_to_run) unless specs_to_run.empty?
end

end

def run_cucumber_scenario scenario_path
run "cucumber #{scenario_path}"
end

# --------------------------------------------------
# Watchr Rules
# --------------------------------------------------
watch( '^spec/spec_helper\.rb' ) { run_all_specs }
watch( '^spec/shared_behaviors/.*\.rb' ) { |m| run_specs_with_shared_examples(m[0]) }
watch( '^spec/.*_spec\.rb' ) { |m| run_single_spec(m[0]) }
watch( '^app/(.*)\.rb' ) { |m| run_single_spec("spec/%s_spec.rb" % m[1]) }
watch( '^app/views/(.*)\.haml' ) { |m| run_single_spec("spec/views/%s.haml_spec.rb" % m[1]) }
watch( '^lib/(.*)\.rb' ) { |m| run_single_spec("spec/lib/%s_spec.rb" % m[1] ) }
watch( '^features/.*\.feature' ) { |m| run_cucumber_scenario(m[0]) }


# --------------------------------------------------
# Signal Handling
# --------------------------------------------------
# Ctrl-\
Signal.trap('QUIT') do
puts " --- Running all tests ---\n\n"
run_all_specs
end

# Ctrl-C
Signal.trap('INT') { abort("\n") }

puts "Watching.."
40 changes: 40 additions & 0 deletions spec/lib/generators/localized_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'spec_helper'
require "generator_spec/test_case"
require 'generators/localized/localized_generator'

describe LocalizedGenerator do
include GeneratorSpec::TestCase
destination File.expand_path("../../tmp", __FILE__)

before do
prepare_destination
run_generator
end

specify do
destination_root.should have_structure {
no_file "sites.rb"
directory "app" do
directory "concerns" do
directory "localized" do
file "sites.rb" do
contains "# this file was originally generated by the localized generator"
contains "module Localized::Sites"
contains "bitfield :sites,"
contains "default_scope"
end
end
end
end
no_file "localized.yml"
directory "config" do
file "localized.yml" do
contains "# this file was originally generated by the localized generator"
contains "default_site"
contains "us: 'en-US'"
end
end
}
end
end

0 comments on commit 63dc12c

Please sign in to comment.