mongoid_i18n_embedded 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/mongoid/i18n.rb +23 -1
  2. metadata +4 -4
data/lib/mongoid/i18n.rb CHANGED
@@ -10,6 +10,8 @@ module Mongoid
10
10
  Mongoid::I18n::InternationalizedData.generate(self)
11
11
  embeds_many :internationalized_data, :class_name => "#{self.name}::InternationalizedData"
12
12
  accepts_nested_attributes_for :internationalized_data
13
+
14
+ after_initialize :generates_internationalized_data
13
15
  end
14
16
 
15
17
  class << self
@@ -21,6 +23,10 @@ module Mongoid
21
23
  yield(@configuration)
22
24
  end
23
25
 
26
+ def self.locales
27
+ Mongoid::I18n.configuration.locales
28
+ end
29
+
24
30
  module ClassMethods
25
31
  def internationalized_field(name, options = {})
26
32
  "#{self.name}::InternationalizedData".constantize.field name, options
@@ -30,7 +36,7 @@ module Mongoid
30
36
  i18n_data.nil? ? "" : i18n_data.send(name)
31
37
  end
32
38
 
33
- Mongoid::I18n.configuration.locales.each do |l|
39
+ Mongoid::I18n.locales.each do |l|
34
40
  n = "#{name}_#{l.to_s.gsub('-', '_')}"
35
41
  define_method(n) do
36
42
  i18n_data ||= self.internationalized_data.where(:language => l.to_s).first
@@ -38,9 +44,25 @@ module Mongoid
38
44
  end
39
45
  end
40
46
  end
47
+
48
+ def validates_internationalized_fields(*fields)
49
+ validates_each fields do |record, attr, value|
50
+ record.internationalized_data.each do |i18n_data|
51
+ record.errors.add attr, "#{i18n_data.language} #{::I18n.translate('errors.messages.blank')}" if i18n_data.send(attr).blank?
52
+ end
53
+ end
54
+ end
41
55
  end
42
56
 
43
57
  module InstanceMethods
58
+ def generates_internationalized_data
59
+ Mongoid::I18n.locales.each do |l|
60
+ if internationalized_data.where(:language => l.to_s).count < 1
61
+ internationalized_data.build(:language => l.to_s)
62
+ end
63
+ end
64
+ end
44
65
  end
66
+
45
67
  end
46
68
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_i18n_embedded
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2011-11-07 00:00:00.000000000 Z
13
+ date: 2011-11-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: mongoid
17
- requirement: &2153538220 !ruby/object:Gem::Requirement
17
+ requirement: &2152983220 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,7 +22,7 @@ dependencies:
22
22
  version: '2.2'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *2153538220
25
+ version_requirements: *2152983220
26
26
  description: This gem provides functionalities to manipulate internationalized data
27
27
  using Mongoid.
28
28
  email: contato@lucasrenan.com