-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added rails 3 generator for creating localized.yml and the localized …
…concerns used within Active Record models
- Loading branch information
1 parent
5f967a9
commit 63dc12c
Showing
6 changed files
with
181 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|