awesome_translations 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +30 -0
- data/Rakefile +23 -0
- data/app/assets/javascripts/awesome_translations/application.js +13 -0
- data/app/assets/stylesheets/awesome_translations/application.css.scss +14 -0
- data/app/assets/stylesheets/awesome_translations/groups.css.scss +18 -0
- data/app/assets/stylesheets/awesome_translations/layout.css.scss +51 -0
- data/app/controllers/awesome_translations/application_controller.rb +4 -0
- data/app/controllers/awesome_translations/groups_controller.rb +38 -0
- data/app/controllers/awesome_translations/handlers_controller.rb +17 -0
- data/app/controllers/awesome_translations/translations_controller.rb +4 -0
- data/app/helpers/awesome_translations/application_helper.rb +4 -0
- data/app/models/awesome_translations/group.rb +43 -0
- data/app/models/awesome_translations/handler.rb +45 -0
- data/app/models/awesome_translations/translated_value.rb +47 -0
- data/app/models/awesome_translations/translation.rb +88 -0
- data/app/views/awesome_translations/groups/show.html.haml +28 -0
- data/app/views/awesome_translations/handlers/index.html.haml +13 -0
- data/app/views/awesome_translations/handlers/show.html.haml +12 -0
- data/app/views/layouts/awesome_translations/application.html.haml +24 -0
- data/config/routes.rb +7 -0
- data/lib/awesome_translations.rb +18 -0
- data/lib/awesome_translations/config.rb +7 -0
- data/lib/awesome_translations/engine.rb +3 -0
- data/lib/awesome_translations/erb_inspector.rb +45 -0
- data/lib/awesome_translations/erb_inspector/file_inspector.rb +88 -0
- data/lib/awesome_translations/erb_inspector/translation_inspector.rb +69 -0
- data/lib/awesome_translations/handlers.rb +25 -0
- data/lib/awesome_translations/handlers/base_handler.rb +2 -0
- data/lib/awesome_translations/handlers/erb_handler.rb +35 -0
- data/lib/awesome_translations/handlers/global_handler.rb +31 -0
- data/lib/awesome_translations/handlers/model_handler.rb +95 -0
- data/lib/awesome_translations/handlers/validations_handler.rb +70 -0
- data/lib/awesome_translations/model_inspector.rb +137 -0
- data/lib/awesome_translations/model_inspector/attribute.rb +12 -0
- data/lib/awesome_translations/object_extensions.rb +13 -0
- data/lib/awesome_translations/version.rb +3 -0
- data/lib/tasks/awesome_translations_tasks.rake +29 -0
- data/spec/controllers/groups_controller_spec.rb +34 -0
- data/spec/controllers/handlers_controller_spec.rb +16 -0
- data/spec/controllers/users_controller_spec.rb +18 -0
- data/spec/dummy/README.rdoc +28 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/javascripts/application.js +15 -0
- data/spec/dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/dummy/app/controllers/application_controller.rb +17 -0
- data/spec/dummy/app/controllers/locales_controller.rb +6 -0
- data/spec/dummy/app/controllers/users_controller.rb +32 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/models/role.rb +4 -0
- data/spec/dummy/app/models/user.rb +7 -0
- data/spec/dummy/app/views/layouts/application.html.haml +21 -0
- data/spec/dummy/app/views/users/_partial_test.html.haml +1 -0
- data/spec/dummy/app/views/users/index.html.haml +16 -0
- data/spec/dummy/app/views/users/show.html.erb +6 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +28 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +32 -0
- data/spec/dummy/config/environments/production.rb +80 -0
- data/spec/dummy/config/environments/test.rb +39 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +12 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/awesome_translations/app/views/users/da.yml +5 -0
- data/spec/dummy/config/locales/awesome_translations/app/views/users/de.yml +5 -0
- data/spec/dummy/config/locales/awesome_translations/app/views/users/en.yml +5 -0
- data/spec/dummy/config/locales/awesome_translations/da.yml +4 -0
- data/spec/dummy/config/locales/awesome_translations/de.yml +4 -0
- data/spec/dummy/config/locales/awesome_translations/en.yml +4 -0
- data/spec/dummy/config/locales/awesome_translations/models/role/da.yml +6 -0
- data/spec/dummy/config/locales/awesome_translations/models/role/de.yml +6 -0
- data/spec/dummy/config/locales/awesome_translations/models/role/en.yml +6 -0
- data/spec/dummy/config/locales/awesome_translations/models/user/da.yml +12 -0
- data/spec/dummy/config/locales/awesome_translations/models/user/de.yml +12 -0
- data/spec/dummy/config/locales/awesome_translations/models/user/en.yml +12 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +7 -0
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/migrate/20141018081559_create_users.rb +9 -0
- data/spec/dummy/db/migrate/20141018081640_create_roles.rb +10 -0
- data/spec/dummy/db/schema.rb +29 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/development.log +16953 -0
- data/spec/dummy/log/test.log +4992 -0
- data/spec/dummy/public/404.html +58 -0
- data/spec/dummy/public/422.html +58 -0
- data/spec/dummy/public/500.html +57 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/tmp/cache/assets/development/sass/79bc6a1530be4e47bb2bf0dda4943bc0e439789f/application.css.scssc +0 -0
- data/spec/dummy/tmp/cache/assets/development/sass/79bc6a1530be4e47bb2bf0dda4943bc0e439789f/groups.css.scssc +0 -0
- data/spec/dummy/tmp/cache/assets/development/sass/79bc6a1530be4e47bb2bf0dda4943bc0e439789f/layout.css.scssc +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/0cc5658eb161251c8bd64e1cf25e2191 +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/1dfc909a73e53e47b341f67cfd8a3fc3 +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/2128d293dc8ef13115c1f5d64b5acfc9 +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/3bd5629974bb1919d009bcb1da7a7e49 +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/50b9bcf05a8718d9cb5b99276f154ea9 +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/5316fc0620afefc2ba81b690c8e2c34d +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/538966109f3bbd58bfb717ad57a37096 +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/58b9ed99ad62ba462ca6a9272b96bef3 +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/697d977cf9fc7e54534f5e29b0832836 +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/6aab60b365535cc305f37a738e75868b +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/6db8498274efddac6691248cc9afe855 +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/81ab882fef1228d654a7c697f2324309 +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/908bf508cc68079cab720d6b9c36cb95 +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/91090eda0f104056144ac17a4fcae91c +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/a3c8104e5830cd7047c6a3a1c1e2ba32 +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/b512f2b016bd3aef8c28fa661e01308c +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/bd49bb0a6f5dfd284a750fb9cab40d85 +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/c39fce5fa19b758734af29527d4185ca +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/c8ecba0dbc7774cd6db79cb7fbe1ef3e +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/cb595904fcf7acdedf2b95571442fc59 +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/eb9ed717a7ef467bf260b11dfc8451eb +0 -0
- data/spec/dummy/tmp/cache/assets/development/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sass/79bc6a1530be4e47bb2bf0dda4943bc0e439789f/application.css.scssc +0 -0
- data/spec/dummy/tmp/cache/assets/test/sass/79bc6a1530be4e47bb2bf0dda4943bc0e439789f/groups.css.scssc +0 -0
- data/spec/dummy/tmp/cache/assets/test/sass/79bc6a1530be4e47bb2bf0dda4943bc0e439789f/layout.css.scssc +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/2128d293dc8ef13115c1f5d64b5acfc9 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/50b9bcf05a8718d9cb5b99276f154ea9 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/5316fc0620afefc2ba81b690c8e2c34d +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/538966109f3bbd58bfb717ad57a37096 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/58b9ed99ad62ba462ca6a9272b96bef3 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/697d977cf9fc7e54534f5e29b0832836 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/6aab60b365535cc305f37a738e75868b +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/6db8498274efddac6691248cc9afe855 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/81ab882fef1228d654a7c697f2324309 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/908bf508cc68079cab720d6b9c36cb95 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/91090eda0f104056144ac17a4fcae91c +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/a3c8104e5830cd7047c6a3a1c1e2ba32 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/b512f2b016bd3aef8c28fa661e01308c +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/bd49bb0a6f5dfd284a750fb9cab40d85 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/c39fce5fa19b758734af29527d4185ca +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/cb595904fcf7acdedf2b95571442fc59 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/eb9ed717a7ef467bf260b11dfc8451eb +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
- data/spec/factories/user.rb +7 -0
- data/spec/handlers/erb_handler_spec.rb +35 -0
- data/spec/handlers/global_handler_spec.rb +19 -0
- data/spec/handlers/model_handler_spec.rb +30 -0
- data/spec/handlers/validations_handler_spec.rb +25 -0
- data/spec/handlers_spec.rb +9 -0
- data/spec/lib/erb_inspector/file_inspector_spec.rb +21 -0
- data/spec/lib/erb_inspector/translation_inspector_spec.rb +23 -0
- data/spec/lib/erb_inspector_spec.rb +26 -0
- data/spec/models/awesome_translations/handler_spec.rb +8 -0
- data/spec/models/awesome_translations/translated_value_spec.rb +43 -0
- data/spec/spec_helper.rb +56 -0
- metadata +505 -0
@@ -0,0 +1,70 @@
|
|
1
|
+
class AwesomeTranslations::Handlers::ValidationsHandler < AwesomeTranslations::Handlers::BaseHandler
|
2
|
+
def groups
|
3
|
+
ArrayEnumerator.new do |yielder|
|
4
|
+
AwesomeTranslations::ModelInspector.model_classes.each do |model_inspector|
|
5
|
+
yielder << AwesomeTranslations::Group.new(
|
6
|
+
id: model_inspector.clazz.name,
|
7
|
+
handler: self
|
8
|
+
)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def translations_for_group(group)
|
14
|
+
ArrayEnumerator.new do |yielder|
|
15
|
+
model_inspector = AwesomeTranslations::ModelInspector.model_classes.select { |model_inspector| model_inspector.clazz.name == group.name }.first
|
16
|
+
raise "No inspector by that name: #{model_inspector.clazz.name}" unless model_inspector
|
17
|
+
|
18
|
+
model_inspector.clazz._validators.each do |attribute_name, validators|
|
19
|
+
validators.each do |validator|
|
20
|
+
translations_for_length_validator(validator, model_inspector, attribute_name, yielder) if validator.is_a?(ActiveModel::Validations::LengthValidator)
|
21
|
+
translations_for_format_validator(validator, model_inspector, attribute_name, yielder) if validator.is_a?(ActiveModel::Validations::FormatValidator)
|
22
|
+
translations_for_uniqueness_validator(validator, model_inspector, attribute_name, yielder) if validator.is_a?(ActiveRecord::Validations::UniquenessValidator)
|
23
|
+
translations_for_presence_validator(validator, model_inspector, attribute_name, yielder) if validator.is_a?(ActiveRecord::Validations::PresenceValidator)
|
24
|
+
translations_for_email_validator(validator, model_inspector, attribute_name, yielder) if validator.class.name == "EmailValidator"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def translations_for_length_validator(validator, model_inspector, attribute_name, yielder)
|
33
|
+
if validator.options[:minimum].present?
|
34
|
+
add_translation("too_short", model_inspector, attribute_name, yielder) # "is too short. The minimum is %{count}"
|
35
|
+
end
|
36
|
+
|
37
|
+
if validator.options[:maximum].present?
|
38
|
+
add_translation("too_long", model_inspector, attribute_name, yielder) # "is too long. The maximum is %{count}"
|
39
|
+
end
|
40
|
+
|
41
|
+
if validator.options[:is].present?
|
42
|
+
add_translation("wrong_length", model_inspector, attribute_name, yielder) # "is not the correct length: %{count}"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def translations_for_format_validator(validator, model_inspector, attribute_name, yielder)
|
47
|
+
add_translation("invalid", model_inspector, attribute_name, yielder) # "is invalid"
|
48
|
+
end
|
49
|
+
|
50
|
+
def translations_for_uniqueness_validator(validator, model_inspector, attribute_name, yielder)
|
51
|
+
add_translation("taken", model_inspector, attribute_name, yielder) # "has already been taken"
|
52
|
+
end
|
53
|
+
|
54
|
+
def translations_for_presence_validator(validator, model_inspector, attribute_name, yielder)
|
55
|
+
add_translation("blank", model_inspector, attribute_name, yielder) # "cannot be blank"
|
56
|
+
end
|
57
|
+
|
58
|
+
def translations_for_email_validator(validator, model_inspector, attribute_name, yielder)
|
59
|
+
add_translation("invalid", model_inspector, attribute_name, yielder) # "is invalid"
|
60
|
+
end
|
61
|
+
|
62
|
+
def add_translation(key, model_inspector, attribute_name, yielder)
|
63
|
+
snake_clazz_name = StringCases.camel_to_snake(model_inspector.clazz.name)
|
64
|
+
|
65
|
+
yielder << AwesomeTranslations::Translation.new(
|
66
|
+
key: "activerecord.errors.models.#{snake_clazz_name}.attributes.#{attribute_name}.#{key}",
|
67
|
+
dir: "#{Rails.root}/config/locales/awesome_translations/models/#{snake_clazz_name}"
|
68
|
+
)
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,137 @@
|
|
1
|
+
class AwesomeTranslations::ModelInspector
|
2
|
+
autoload :Attribute, "#{File.dirname(__FILE__)}/model_inspector/attribute"
|
3
|
+
|
4
|
+
attr_reader :clazz
|
5
|
+
|
6
|
+
# Yields a model-inspector for each model found in the application.
|
7
|
+
def self.model_classes
|
8
|
+
# Make sure all models are loaded.
|
9
|
+
load_models
|
10
|
+
|
11
|
+
@scanned = {}
|
12
|
+
@yielded = {}
|
13
|
+
|
14
|
+
Enumerator.new do |yielder|
|
15
|
+
constants = Module.constants + Object.constants + Kernel.constants
|
16
|
+
constants.sort.each do |constant_name|
|
17
|
+
scan_for_models(::Object, constant_name) do |model_inspector|
|
18
|
+
yielder << model_inspector
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def initialize(clazz)
|
25
|
+
@clazz = clazz
|
26
|
+
end
|
27
|
+
|
28
|
+
def attributes
|
29
|
+
@clazz.attribute_names.each do |attribute_name|
|
30
|
+
yield ::AwesomeTranslations::ModelInspector::Attribute.new(self, attribute_name)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def paperclip_attachments
|
35
|
+
return [] unless ::Kernel.const_defined?("Paperclip")
|
36
|
+
Paperclip::AttachmentRegistry.names_for(@clazz).each do |name|
|
37
|
+
yield name
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def snake_name
|
42
|
+
clazz.name.gsub("::", "/").split("/").map { |part| ::StringCases.camel_to_snake(part) }.join("/")
|
43
|
+
end
|
44
|
+
|
45
|
+
def gettext_key
|
46
|
+
return "activerecord.models.#{snake_name}"
|
47
|
+
end
|
48
|
+
|
49
|
+
def gettext_key_one
|
50
|
+
return "#{gettext_key}.one"
|
51
|
+
end
|
52
|
+
|
53
|
+
def gettext_key_other
|
54
|
+
return "#{gettext_key}.other"
|
55
|
+
end
|
56
|
+
|
57
|
+
# TODO: Maybe this should yield a ModelInspector::Relationship instead?
|
58
|
+
def relationships
|
59
|
+
@clazz.reflections.each do |key, reflection|
|
60
|
+
yield key, reflection
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def i18n_key name
|
65
|
+
return "activerecord.attributes.#{snake_name}.#{name}"
|
66
|
+
end
|
67
|
+
|
68
|
+
def to_s
|
69
|
+
"<AwesomeTranslations::ModelInspector class-name: \"#{@clazz.name}\">"
|
70
|
+
end
|
71
|
+
|
72
|
+
def inspect
|
73
|
+
to_s
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
# Loads all models for Rails app and all engines.
|
79
|
+
def self.load_models
|
80
|
+
load_models_for(Rails.root)
|
81
|
+
Rails.application.railties.engines.each do |r|
|
82
|
+
load_models_for(r.root)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# Loads models for the given app-directory (Rails-root or engine).
|
87
|
+
def self.load_models_for(root)
|
88
|
+
Dir.glob("#{root}/app/models/**/*.rb") do |model_path|
|
89
|
+
require model_path
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def self.scan_for_models(parent_clazz, constant_name, &blk)
|
94
|
+
return unless parent_clazz.const_defined?(constant_name)
|
95
|
+
|
96
|
+
begin
|
97
|
+
clazz = parent_clazz.const_get(constant_name)
|
98
|
+
rescue
|
99
|
+
return
|
100
|
+
end
|
101
|
+
|
102
|
+
return if !clazz.respond_to?(:name) || !clazz.name || !clazz.respond_to?(:constants)
|
103
|
+
return if clazz.name == "ActiveRecord::SchemaMigration"
|
104
|
+
return if clazz.name.end_with?("::Translation")
|
105
|
+
|
106
|
+
clazz.constants.sort.each do |clazz_sym|
|
107
|
+
begin
|
108
|
+
class_current = clazz.const_get(clazz_sym)
|
109
|
+
rescue
|
110
|
+
next
|
111
|
+
rescue RuntimeError, LoadError
|
112
|
+
next
|
113
|
+
end
|
114
|
+
|
115
|
+
if @scanned[class_current]
|
116
|
+
next
|
117
|
+
else
|
118
|
+
@scanned[class_current] = true
|
119
|
+
end
|
120
|
+
|
121
|
+
next if !class_current.is_a?(Class) && !class_current.is_a?(Module)
|
122
|
+
|
123
|
+
scan_for_models(clazz, clazz_sym, &blk)
|
124
|
+
end
|
125
|
+
|
126
|
+
begin
|
127
|
+
return unless clazz < ActiveRecord::Base
|
128
|
+
rescue
|
129
|
+
return
|
130
|
+
end
|
131
|
+
|
132
|
+
unless @yielded[clazz]
|
133
|
+
@yielded[clazz] = true
|
134
|
+
blk.call ::AwesomeTranslations::ModelInspector.new(clazz)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class AwesomeTranslations::ModelInspector::Attribute
|
2
|
+
attr_reader :name
|
3
|
+
|
4
|
+
def initialize(clazz_inspector, name)
|
5
|
+
@clazz_inspector = clazz_inspector
|
6
|
+
@name = name
|
7
|
+
end
|
8
|
+
|
9
|
+
def i18n_key
|
10
|
+
return "activerecord.attributes.#{@clazz_inspector.snake_name}.#{@name}"
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# desc "Explaining what the task does"
|
2
|
+
# task :awesome_translations do
|
3
|
+
# # Task goes here
|
4
|
+
# end
|
5
|
+
|
6
|
+
namespace :awesome_translations do
|
7
|
+
task "install" => :environment do
|
8
|
+
yml_root = "#{Rails.root}/config/locales/awesome_translations"
|
9
|
+
|
10
|
+
dirs = [
|
11
|
+
yml_root,
|
12
|
+
"#{yml_root}/helpers",
|
13
|
+
"#{yml_root}/models",
|
14
|
+
"#{yml_root}/views",
|
15
|
+
"#{yml_root}/controllers"
|
16
|
+
]
|
17
|
+
|
18
|
+
dirs.each do |dir|
|
19
|
+
unless File.exists?(dir)
|
20
|
+
puts "[AwesomeTranslations] Creating dir: #{dir}"
|
21
|
+
Dir.mkdir(dir)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
task "update" => :environment do
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe AwesomeTranslations::GroupsController do
|
4
|
+
let(:model_locales_path) { "#{Rails.root}/config/locales/awesome_translations/models" }
|
5
|
+
let(:user_yml_path) { "#{model_locales_path}/user/da.yml" }
|
6
|
+
let(:role_yml_path) { "#{model_locales_path}/role/da.yml" }
|
7
|
+
|
8
|
+
before do
|
9
|
+
FileUtils.rm_f(model_locales_path) if File.exist?(model_locales_path)
|
10
|
+
end
|
11
|
+
|
12
|
+
render_views
|
13
|
+
|
14
|
+
describe "#update" do
|
15
|
+
it "updates translations" do
|
16
|
+
I18n.load_path.should_not include model_locales_path
|
17
|
+
|
18
|
+
put :update, handler_id: "model_handler", id: "User", use_route: "awesome_translations", t: {
|
19
|
+
"activerecord.attributes.user.password" => {"da" => "Adgangskode", "de" => "Kenwort", "en" => "Password"}
|
20
|
+
}
|
21
|
+
|
22
|
+
danish_user_translations = YAML.load_file(user_yml_path)
|
23
|
+
danish_user_translations["da"]["activerecord"]["attributes"]["user"]["password"].should eq "Adgangskode"
|
24
|
+
end
|
25
|
+
|
26
|
+
it "updates paths" do
|
27
|
+
put :update, handler_id: "model_handler", id: "Role", use_route: "awesome_translations", t: {
|
28
|
+
"activerecord.attributes.role.role" => {"da" => "Rolle", "de" => "Die type", "en" => "Role"}
|
29
|
+
}
|
30
|
+
|
31
|
+
I18n.load_path.should include role_yml_path
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "fileutils"
|
3
|
+
|
4
|
+
describe AwesomeTranslations::HandlersController do
|
5
|
+
render_views
|
6
|
+
|
7
|
+
it "#index" do
|
8
|
+
get :index, use_route: "awesome_translations"
|
9
|
+
response.should be_success
|
10
|
+
end
|
11
|
+
|
12
|
+
it "#show" do
|
13
|
+
get :show, id: "model_handler", use_route: "awesome_translations"
|
14
|
+
response.should be_success
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe UsersController do
|
4
|
+
let(:user) { create :user }
|
5
|
+
|
6
|
+
render_views
|
7
|
+
|
8
|
+
before do
|
9
|
+
I18n.locale = :en
|
10
|
+
end
|
11
|
+
|
12
|
+
it "#index" do
|
13
|
+
user
|
14
|
+
get :index
|
15
|
+
response.should be_success
|
16
|
+
assigns(:users).should include user
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
== README
|
2
|
+
|
3
|
+
This README would normally document whatever steps are necessary to get the
|
4
|
+
application up and running.
|
5
|
+
|
6
|
+
Things you may want to cover:
|
7
|
+
|
8
|
+
* Ruby version
|
9
|
+
|
10
|
+
* System dependencies
|
11
|
+
|
12
|
+
* Configuration
|
13
|
+
|
14
|
+
* Database creation
|
15
|
+
|
16
|
+
* Database initialization
|
17
|
+
|
18
|
+
* How to run the test suite
|
19
|
+
|
20
|
+
* Services (job queues, cache servers, search engines, etc.)
|
21
|
+
|
22
|
+
* Deployment instructions
|
23
|
+
|
24
|
+
* ...
|
25
|
+
|
26
|
+
|
27
|
+
Please feel free to use a different markup language if you do not plan to run
|
28
|
+
<tt>rake doc:app</tt>.
|
data/spec/dummy/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
14
|
+
//= require jquery
|
15
|
+
//= require jquery_ujs
|
@@ -0,0 +1,13 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the top of the
|
9
|
+
* compiled file, but it's generally better to create a new file per style scope.
|
10
|
+
*
|
11
|
+
*= require_self
|
12
|
+
*= require_tree .
|
13
|
+
*/
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class ApplicationController < ActionController::Base
|
2
|
+
# Prevent CSRF attacks by raising an exception.
|
3
|
+
# For APIs, you may want to use :null_session instead.
|
4
|
+
protect_from_forgery with: :exception
|
5
|
+
|
6
|
+
before_filter :set_locale
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def set_locale
|
11
|
+
if session[:locale]
|
12
|
+
I18n.locale = session[:locale]
|
13
|
+
else
|
14
|
+
I18n.locale = :en
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
class UsersController < ApplicationController
|
2
|
+
before_filter :set_user
|
3
|
+
|
4
|
+
def index
|
5
|
+
@users = User.all
|
6
|
+
end
|
7
|
+
|
8
|
+
def show
|
9
|
+
end
|
10
|
+
|
11
|
+
def new
|
12
|
+
end
|
13
|
+
|
14
|
+
def create
|
15
|
+
if @user.save
|
16
|
+
flash[:notice] = t(".user_was_created")
|
17
|
+
redirect_to user_path(@user)
|
18
|
+
else
|
19
|
+
flash[:error] = @user.errors.full_messages.join(". ")
|
20
|
+
render :new
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def edit
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def set_user
|
30
|
+
@user = User.find(params[:id].to_i) if params[:id].to_i > 0
|
31
|
+
end
|
32
|
+
end
|