translatable 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -72,6 +72,12 @@ Block accepts following methods:
72
72
  OR
73
73
  translatable_locale :lang
74
74
 
75
+ translatable_attr_protected
76
+ If this line is set then the attributes :locale and :origin will be protected from
77
+ mass-assigment, otherwise they will be available.
78
+
79
+ Note: Note sure that this is the best implementation. Would appreciate any better ideas.
80
+
75
81
  == Now to create the translations?
76
82
 
77
83
  They can be created in two different ways:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
data/lib/translatable.rb CHANGED
@@ -107,6 +107,20 @@ module ActiveRecord
107
107
  @translatable[:origin] = translatable_origin_prepared origin_key
108
108
  end
109
109
 
110
+ ###
111
+ # Will not register the attributes as accessible.
112
+ # IMPORTANT: Translatable block will be evaluated on the model after it
113
+ # was loaded, so it will modify certain thing on final version. Hence this thing is needed.
114
+ # Examples:
115
+ #
116
+ # translatable_attr_protected
117
+ #
118
+ # Default: false
119
+ #
120
+ def translatable_attr_protected
121
+ @translatable[:attr_protected] = true
122
+ end
123
+
110
124
  ###
111
125
  # Define the key that the translation will be used for belongs_to association,
112
126
  # to communicate with original model
@@ -195,7 +209,7 @@ module ActiveRecord
195
209
 
196
210
  belongs_to :#{@translatable[:origin]}, :class_name => "#{self.name}"
197
211
 
198
- attr_accessible :#{@translatable[:locale]}, :#{@translatable[:origin]}_id
212
+ attr_#{!!@translatable[:attr_protected] ? "protected" : "accessible" } :#{@translatable[:locale]}, :#{@translatable[:origin]}_id
199
213
  RUBY
200
214
 
201
215
  @translatable[:properties].each do |p|
data/test/messages.rb ADDED
@@ -0,0 +1,72 @@
1
+ require 'active_record'
2
+ require 'translatable'
3
+
4
+ class CreateMessagesTables < ActiveRecord::Migration
5
+ def up
6
+ create_table(:writers, :force => true) do |t|
7
+ t.string :name, :null => false
8
+
9
+ t.timestamps
10
+ end
11
+
12
+ create_table(:translated_messages) do |t|
13
+ t.string :title, :null => false
14
+ t.string :content, :null => false
15
+ t.integer :message_id, :null => false
16
+ t.string :locale, :null => false, :limit => 2
17
+ t.integer :writer_id
18
+
19
+ t.timestamps
20
+ end
21
+
22
+ create_table(:messages) do |t|
23
+ t.integer :writer_id
24
+
25
+ t.timestamps
26
+ end
27
+ end
28
+
29
+ def down
30
+ drop_table(:writers)
31
+ drop_table(:translated_messages)
32
+ drop_table(:messages)
33
+ end
34
+ end
35
+
36
+ CreateMessagesTables.migrate(:up)
37
+
38
+ class Author < ActiveRecord::Base
39
+ validates :name, :presence => true
40
+ end
41
+
42
+ class TranslatedMessage < ActiveRecord::Base
43
+ attr_accessible :title, :content
44
+
45
+ before_validation :set_default_locale, :if => :writer_id
46
+ before_create :duplicate_writer_id, :unless => :writer_id
47
+
48
+ protected
49
+
50
+ def set_default_locale
51
+ self.locale ||= ::I18n.locale
52
+ end
53
+
54
+ def duplicate_writer_id
55
+ self.writer_id = message.writer_id
56
+ end
57
+ end
58
+
59
+ class Message < ActiveRecord::Base
60
+
61
+ belongs_to :writer
62
+
63
+ translatable do
64
+ translatable :title, :presence => true, :uniqueness => true
65
+ translatable :content, :presence => true
66
+ translatable_model 'TranslatedMessage'
67
+ translatable_origin :message
68
+ translatable_attr_protected
69
+ end
70
+
71
+ attr_accessible :writer_id, :writer
72
+ end
data/test/posts.rb CHANGED
@@ -3,7 +3,7 @@ require 'translatable'
3
3
 
4
4
  class CreatePostsTables < ActiveRecord::Migration
5
5
  def up
6
- create_table(:writers) do |t|
6
+ create_table(:writers, :force => true) do |t|
7
7
  t.string :name, :null => false
8
8
 
9
9
  t.timestamps
@@ -2,6 +2,7 @@
2
2
  require 'helper'
3
3
  require 'news'
4
4
  require 'posts'
5
+ require 'messages'
5
6
 
6
7
  class TestDmTranslatable < Test::Unit::TestCase
7
8
 
@@ -218,4 +219,22 @@ class TestDmTranslatable < Test::Unit::TestCase
218
219
 
219
220
  assert_equal post, post.translations.first.post
220
221
  end
222
+
223
+ def test_protected_mass_assigment
224
+ tm = TranslatedMessage.new( :title => "Resent Post", :content => "That is where the text goes", :locale => "en", :message_id => 1)
225
+
226
+ assert_equal "Resent Post", tm.title
227
+ assert_equal "That is where the text goes", tm.content
228
+ assert_equal nil, tm.locale
229
+ assert_equal nil, tm.message_id
230
+ end
231
+
232
+ def test_accessible_mass_assigment
233
+ tp = TranslatableNews.new( :title => "Resent News", :content => "That is where the text goes", :locale => "en", :origin_id => 1)
234
+
235
+ assert_equal "Resent News", tp.title
236
+ assert_equal "That is where the text goes", tp.content
237
+ assert_equal "en", tp.locale
238
+ assert_equal 1, tp.origin_id
239
+ end
221
240
  end
data/translatable.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "translatable"
8
- s.version = "0.1.3"
8
+ s.version = "0.1.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["E-Max"]
12
- s.date = "2012-08-19"
12
+ s.date = "2012-08-25"
13
13
  s.description = "This game was build to make whole proccess of working with translation for DM to be almost invisble. That was THE AIM."
14
14
  s.email = "max@studentify.nl"
15
15
  s.extra_rdoc_files = [
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
28
28
  "VERSION",
29
29
  "lib/translatable.rb",
30
30
  "test/helper.rb",
31
+ "test/messages.rb",
31
32
  "test/news.rb",
32
33
  "test/posts.rb",
33
34
  "test/support/active_record.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: translatable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-19 00:00:00.000000000Z
12
+ date: 2012-08-25 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &74385030 !ruby/object:Gem::Requirement
16
+ requirement: &74173130 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *74385030
24
+ version_requirements: *74173130
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: i18n
27
- requirement: &74384790 !ruby/object:Gem::Requirement
27
+ requirement: &74172880 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *74384790
35
+ version_requirements: *74172880
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: yard
38
- requirement: &74384550 !ruby/object:Gem::Requirement
38
+ requirement: &74172640 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *74384550
46
+ version_requirements: *74172640
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: jeweler
49
- requirement: &74384310 !ruby/object:Gem::Requirement
49
+ requirement: &74172400 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.6.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *74384310
57
+ version_requirements: *74172400
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: bundler
60
- requirement: &74384070 !ruby/object:Gem::Requirement
60
+ requirement: &74172160 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 1.0.0
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *74384070
68
+ version_requirements: *74172160
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: simplecov
71
- requirement: &74383830 !ruby/object:Gem::Requirement
71
+ requirement: &74171880 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: 0.6.0
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *74383830
79
+ version_requirements: *74171880
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rcov
82
- requirement: &74383590 !ruby/object:Gem::Requirement
82
+ requirement: &74171640 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,7 +87,7 @@ dependencies:
87
87
  version: 1.0.0
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *74383590
90
+ version_requirements: *74171640
91
91
  description: This game was build to make whole proccess of working with translation
92
92
  for DM to be almost invisble. That was THE AIM.
93
93
  email: max@studentify.nl
@@ -108,6 +108,7 @@ files:
108
108
  - VERSION
109
109
  - lib/translatable.rb
110
110
  - test/helper.rb
111
+ - test/messages.rb
111
112
  - test/news.rb
112
113
  - test/posts.rb
113
114
  - test/support/active_record.rb
@@ -129,7 +130,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
129
130
  version: '0'
130
131
  segments:
131
132
  - 0
132
- hash: 853174477
133
+ hash: -993677265
133
134
  required_rubygems_version: !ruby/object:Gem::Requirement
134
135
  none: false
135
136
  requirements: