i18n_attributes 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/CHANGELOG ADDED
@@ -0,0 +1,4 @@
1
+ == 0.1.0
2
+
3
+ * First release
4
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in i18n_attributes.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,22 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Rails example
10
+ watch(%r{^spec/.+_spec\.rb$})
11
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
12
+ watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
13
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
14
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
15
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
16
+ watch('spec/spec_helper.rb') { "spec" }
17
+ watch('config/routes.rb') { "spec/routing" }
18
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
19
+ # Capybara request specs
20
+ watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
21
+ end
22
+
data/README.markdown ADDED
@@ -0,0 +1,60 @@
1
+ #I18nAttributes
2
+
3
+ I18nAttributes is a generate model attributes I18n locale files plugin for Rails3.
4
+
5
+ * https://github.com/vkill/i18n_attributes
6
+
7
+ ##Supported versions
8
+
9
+ * Ruby 1.8.7, 1.9.2, 1.9.3
10
+
11
+ * Rails 3.0.x, 3.1
12
+
13
+
14
+ ##Installation
15
+
16
+ In your app's `Gemfile`, add:
17
+
18
+ gem "i18n_attributes", "~> 0.1.0"
19
+
20
+ Then run:
21
+
22
+ > bundle
23
+ > rails generate i18n_attributes:install
24
+
25
+ If your want to configure, see `config/initializers/i18n_attributes.rb`
26
+
27
+
28
+ ##Uninstallation
29
+
30
+ Run:
31
+
32
+ > rails destroy i18n_attributes:install
33
+
34
+
35
+ ##Usage Example
36
+
37
+ When your generate post model, then hook invoke, create `config/locales/model_zh-CN/post.yml` file
38
+
39
+ > rails g model post title:string
40
+ invoke active_record
41
+ create db/migrate/20111119121327_create_posts.rb
42
+ create app/models/post.rb
43
+ invoke test_unit
44
+ create test/unit/post_test.rb
45
+ create test/fixtures/posts.yml
46
+ invoke i18n_attributes
47
+ create config/locales/model_en/post.yml
48
+ create config/locales/model_zh-CN/post.yml
49
+
50
+ If your models has been created, you want generate model attributes i18n locale file,very easy also, run
51
+
52
+ > rails g i18n_attributes:revise_model
53
+ create config/locales/model_en/post.yml
54
+ create config/locales/model_zh-CN/post.yml
55
+
56
+
57
+ ##Copyright
58
+
59
+ Copyright (c) 2011 vkill.net .
60
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "i18n_attributes/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "i18n_attributes"
7
+ s.version = I18nAttributes::VERSION
8
+ s.authors = ["vkill"]
9
+ s.email = ["vkill.net@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = "A generate model attributes I18n locale files plugin for Rails3."
12
+ s.description = "A generate model attributes I18n locale files plugin for Rails3."
13
+
14
+ s.rubyforge_project = "i18n_attributes"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency 'bundler'
22
+ s.add_development_dependency "rake"
23
+ s.add_development_dependency "rspec"
24
+ s.add_development_dependency "guard-rspec"
25
+ s.add_dependency "rails", "~> 3.0"
26
+ end
27
+
@@ -0,0 +1,2 @@
1
+ Description:
2
+
@@ -0,0 +1,29 @@
1
+ class I18nAttributes::InstallGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('../templates', __FILE__)
3
+
4
+ def create_initializer_file
5
+ initializer 'i18n_attributes.rb' do
6
+ %Q/
7
+ I18nAttributes.configure do |config|
8
+ #more > I18n.available_locales
9
+ config.locales = [:en, :"zh-CN"]
10
+ config.enums_attributes = {
11
+ "gender" => {"male" => "Male", "female" => "Female"},
12
+ "state" => {"pending" => "Pending", "processing" => "Processing", "processed" => "Processed"},
13
+ "category" => {"a" => "A", "b" => "B"}
14
+ }
15
+ end
16
+ /
17
+ end
18
+ end
19
+
20
+ def modify_application_config
21
+ inject_into_file 'config/application.rb', :after => /< Rails::Application[\s]*$/ do
22
+ %Q|
23
+ config.i18n.load_path += Dir[%Q`\#{config.root}/config/locales/**/*.{rb,yml}`]
24
+ |
25
+ end
26
+ end
27
+
28
+ end
29
+
@@ -0,0 +1,2 @@
1
+ Description:
2
+
@@ -0,0 +1,27 @@
1
+ class I18nAttributes::ModelGenerator < Rails::Generators::NamedBase
2
+ source_root File.expand_path('../templates', __FILE__)
3
+
4
+ class_option :orm, :required => true
5
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
6
+
7
+ include I18nAttributes::GeneratorHelpers
8
+
9
+ def create_model_i18n_file
10
+
11
+ orm = options.orm.to_s
12
+
13
+ say_error "#{orm} [not found]" unless SUPPORTED_ORMS.include? orm
14
+
15
+ I18nAttributes::Configuration.locales.each do |locale|
16
+ create_file "config/locales/model_#{ locale }/#{ file_name }.yml",
17
+ generate_yaml_file_data(locale, singular_name, human_name, attributes_hash, orm)
18
+ end
19
+
20
+ end
21
+
22
+ private
23
+ def attributes_hash
24
+ Hash[ attributes.map {|attribute| [attribute.name, attribute.human_name] } ]
25
+ end
26
+ end
27
+
@@ -0,0 +1,2 @@
1
+ Description:
2
+
@@ -0,0 +1,42 @@
1
+ class I18nAttributes::ReviseModelGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('../templates', __FILE__)
3
+
4
+ class_option :orm, :required => false, :default => "active_record"
5
+
6
+ include I18nAttributes::GeneratorHelpers
7
+
8
+ SUPPORTED_ORMS = %w(active_record)
9
+
10
+ def create_model_i18n_file
11
+
12
+ orm = options.orm.to_s
13
+
14
+ say_error "#{orm} [not found]" unless SUPPORTED_ORMS.include? orm
15
+
16
+ models do |model|
17
+ I18nAttributes::Configuration.locales.each do |locale|
18
+ create_file "config/locales/model_#{ locale }/#{ model.name.underscore }.yml",
19
+ generate_yaml_file_data(locale, model.model_name.underscore, model.model_name, attributes_hash(model), orm)
20
+ end
21
+ end
22
+
23
+ end
24
+
25
+ private
26
+ def models(&block)
27
+ Rails.application.eager_load!
28
+ ActiveRecord::Base.descendants.each do |model|
29
+ next unless model.respond_to?(:columns_hash)
30
+ next unless model.table_exists?
31
+ next if model.abstract_class?
32
+ next if model.columns_hash.keys.include?('type') and model.descendants.size > 0
33
+ yield model
34
+ end
35
+ end
36
+
37
+ def attributes_hash(model)
38
+ Hash[ model.columns_hash.map { |k,v| [k, k.to_s.humanize] } ]
39
+ end
40
+
41
+ end
42
+
@@ -0,0 +1,14 @@
1
+ module I18nAttributes
2
+ Configuration = Struct.new(
3
+ :locales,
4
+ :enums_attributes
5
+ ).new(
6
+ [:en],
7
+ {
8
+ "gender" => {"male" => "Male", "female" => "Female"},
9
+ "state" => {"pending" => "Pending", "processing" => "Processing", "processed" => "Processed"},
10
+ "category" => {"a" => "A", "b" => "B"}
11
+ }
12
+ )
13
+ end
14
+
@@ -0,0 +1,56 @@
1
+ require "yaml"
2
+
3
+ module I18nAttributes
4
+ module GeneratorHelpers
5
+
6
+ SUPPORTED_ORMS = %w(active_record active_model mongoid)
7
+
8
+ def say_custom(type='warn', text) #example, type choise in FATAL,ERROR,WARN,INFO,DEBUG
9
+ ansicolor = #more ansicolor => https://github.com/flori/term-ansicolor
10
+ case type.downcase.to_s
11
+ when 'fatal'; '31m'
12
+ when 'error'; '31m'
13
+ when 'warn'; '36m'
14
+ when 'info'; '32m'
15
+ when 'debug'; '34m'
16
+ else; '0m'
17
+ end
18
+ say "\033[1m\033[#{ ansicolor }" + type.to_s.upcase.rjust(10) + "\033[0m" + " #{text}"
19
+ end
20
+ def say_fatal(text); say_custom('fatal', text); exit; end
21
+ def say_error(text); say_custom('error', text); exit; end
22
+ def say_warn(text); say_custom('warn', text); end
23
+ def say_info(text); say_custom('info', text); end
24
+ def say_debug(text); say_custom('debug', text); end
25
+
26
+
27
+ def generate_yaml_file_data(locale, singular_name, human_name, attributes_hash, orm="active_record")
28
+ YAML.dump_stream(
29
+ {
30
+ locale.to_s => {
31
+ orm.to_s => {
32
+ "models" => {
33
+ singular_name => human_name.to_s
34
+ },
35
+ "attributes" => {
36
+ singular_name => attributes_hash.merge(enums_hash(attributes_hash.keys))
37
+ }
38
+ }
39
+ }
40
+ }
41
+ )
42
+ end
43
+
44
+ private
45
+
46
+ def enums_hash(attributes)
47
+ enums = {}
48
+ I18nAttributes::Configuration.enums_attributes.each do |k,v|
49
+ attributes.index(k) ? enums.merge!(k => v) : next
50
+ end
51
+ enums.empty? ? {} : { "enums" => enums }
52
+ end
53
+
54
+ end
55
+ end
56
+
@@ -0,0 +1,11 @@
1
+ module SeedUpgrade
2
+ class Railtie < ::Rails::Railtie
3
+ generators do
4
+ require File.expand_path("../../generators/i18n_attributes/model/model_generator", __FILE__)
5
+ require 'rails/generators/rails/model/model_generator'
6
+ Rails::Generators::ModelGenerator.send(:hook_for, :i18n_attributes)
7
+ Rails::Generators::ModelGenerator.send(:class_option, :i18n_attributes, :default => true)
8
+ end
9
+ end
10
+ end
11
+
@@ -0,0 +1,4 @@
1
+ module I18nAttributes
2
+ VERSION = "0.1.0"
3
+ end
4
+
@@ -0,0 +1,13 @@
1
+ require "i18n_attributes/version"
2
+
3
+ require "i18n_attributes/configuration"
4
+ require "rails"
5
+ require "i18n_attributes/railtie"
6
+ require "i18n_attributes/generator_helpers"
7
+
8
+ module I18nAttributes
9
+ def self.configure
10
+ yield Configuration
11
+ end
12
+ end
13
+
@@ -0,0 +1,6 @@
1
+ #encoding: utf-8
2
+
3
+ require "rubygems"
4
+ require "bundler/setup"
5
+ require "i18n_attributes"
6
+
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i18n_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - vkill
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: &77307330 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *77307330
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &77307120 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *77307120
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &77306910 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *77306910
47
+ - !ruby/object:Gem::Dependency
48
+ name: guard-rspec
49
+ requirement: &77306700 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *77306700
58
+ - !ruby/object:Gem::Dependency
59
+ name: rails
60
+ requirement: &77306450 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: '3.0'
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: *77306450
69
+ description: A generate model attributes I18n locale files plugin for Rails3.
70
+ email:
71
+ - vkill.net@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - CHANGELOG
78
+ - Gemfile
79
+ - Guardfile
80
+ - README.markdown
81
+ - Rakefile
82
+ - i18n_attributes.gemspec
83
+ - lib/generators/i18n_attributes/install/USAGE
84
+ - lib/generators/i18n_attributes/install/install_generator.rb
85
+ - lib/generators/i18n_attributes/model/USAGE
86
+ - lib/generators/i18n_attributes/model/model_generator.rb
87
+ - lib/generators/i18n_attributes/revise_model/USAGE
88
+ - lib/generators/i18n_attributes/revise_model/revise_model_generator.rb
89
+ - lib/i18n_attributes.rb
90
+ - lib/i18n_attributes/configuration.rb
91
+ - lib/i18n_attributes/generator_helpers.rb
92
+ - lib/i18n_attributes/railtie.rb
93
+ - lib/i18n_attributes/version.rb
94
+ - spec/spec_helper.rb
95
+ homepage: ''
96
+ licenses: []
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project: i18n_attributes
115
+ rubygems_version: 1.8.10
116
+ signing_key:
117
+ specification_version: 3
118
+ summary: A generate model attributes I18n locale files plugin for Rails3.
119
+ test_files:
120
+ - spec/spec_helper.rb
121
+ has_rdoc: