active_presenter 1.2.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -36,12 +36,35 @@ module ActivePresenter
36
36
  end
37
37
  end
38
38
 
39
- def self.human_attribute_name(attribute_name)
39
+ def self.human_attribute_name(attribute_key_name, options = {})
40
40
  presentable_type = presented.keys.detect do |type|
41
- attribute_name.to_s.starts_with?("#{type}_")
41
+ attribute_key_name.to_s.starts_with?("#{type}_") || attribute_key_name.to_s == type.to_s
42
42
  end
43
+ attribute_key_name_without_class = attribute_key_name.to_s.gsub("#{presentable_type}_", "")
43
44
 
44
- attribute_name.to_s.gsub("#{presentable_type}_", "").humanize
45
+ if presented[presentable_type] and attribute_key_name_without_class != presentable_type.to_s
46
+ presented[presentable_type].human_attribute_name(attribute_key_name_without_class, options)
47
+ else
48
+ I18n.translate(presentable_type, options.merge(:default => presentable_type.to_s.humanize, :scope => [:activerecord, :models]))
49
+ end
50
+ end
51
+
52
+ # Since ActivePresenter does not descend from ActiveRecord, we need to
53
+ # mimic some ActiveRecord behavior in order for the ActiveRecord::Errors
54
+ # object we're using to work properly.
55
+ #
56
+ # This problem was introduced with Rails 2.3.4.
57
+ # Fix courtesy http://gist.github.com/191263
58
+ def self.self_and_descendants_from_active_record # :nodoc:
59
+ [self]
60
+ end
61
+
62
+ def self.human_name(options = {}) # :nodoc:
63
+ defaults = self_and_descendants_from_active_record.map do |klass|
64
+ :"#{klass.name.underscore}"
65
+ end
66
+ defaults << self.name.humanize
67
+ I18n.translate(defaults.shift, {:scope => [:activerecord, :models], :count => 1, :default => defaults}.merge(options))
45
68
  end
46
69
 
47
70
  # Accepts arguments in two forms. For example, if you had a SignupPresenter that presented User, and Account, you could specify arguments in the following two forms:
@@ -60,7 +83,8 @@ module ActivePresenter
60
83
  args ||= {}
61
84
 
62
85
  presented.each do |type, klass|
63
- send("#{type}=", args[type].is_a?(klass) ? args.delete(type) : klass.new)
86
+ value = args.delete(type)
87
+ send("#{type}=", value.is_a?(klass) ? value : klass.new)
64
88
  end
65
89
 
66
90
  self.attributes = args
@@ -71,6 +95,8 @@ module ActivePresenter
71
95
  # the multiparameter attribute form (i.e. {user_birthday(1i) => "1980", user_birthday(2i) => "3"})
72
96
  #
73
97
  def attributes=(attrs)
98
+ return if attrs.nil?
99
+
74
100
  multi_parameter_attributes = {}
75
101
 
76
102
  attrs.each do |k,v|
@@ -90,7 +116,7 @@ module ActivePresenter
90
116
 
91
117
  # Makes sure that the presenter is accurate about responding to presentable's attributes, even though they are handled by method_missing.
92
118
  #
93
- def respond_to?(method)
119
+ def respond_to?(method, include_private = false)
94
120
  presented_attribute?(method) || super
95
121
  end
96
122
 
@@ -222,7 +248,11 @@ module ActivePresenter
222
248
 
223
249
  def merge_errors(presented_inst, type)
224
250
  presented_inst.errors.each do |att,msg|
225
- errors.add(attribute_prefix(type)+att, msg)
251
+ if att == 'base'
252
+ errors.add(type, msg)
253
+ else
254
+ errors.add(attribute_prefix(type)+att, msg)
255
+ end
226
256
  end
227
257
  end
228
258
 
@@ -2,7 +2,7 @@ module ActivePresenter
2
2
  module VERSION
3
3
  MAJOR = 1
4
4
  MINOR = 2
5
- TINY = 0
5
+ TINY = 1
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
data/test/base_test.rb CHANGED
@@ -17,6 +17,10 @@ Expectations do
17
17
  SignupPresenter.new(:user => u.expected).user
18
18
  end
19
19
 
20
+ expect true do
21
+ SignupPresenter.new(:user => nil).user.new_record?
22
+ end
23
+
20
24
  expect User do
21
25
  SignupPresenter.new.user
22
26
  end
@@ -65,6 +69,44 @@ Expectations do
65
69
  s.errors.on(:user_login)
66
70
  end
67
71
 
72
+ expect "can't be blank" do
73
+ s = SignupPresenter.new
74
+ s.valid?
75
+ s.errors.on(:user_login)
76
+ end
77
+
78
+ expect ["User Password can't be blank"] do
79
+ s = SignupPresenter.new(:user_login => 'login')
80
+ s.valid?
81
+ s.errors.full_messages
82
+ end
83
+
84
+ expect 'c4N n07 83 8L4nK' do
85
+ old_locale = I18n.locale
86
+ I18n.locale = '1337'
87
+
88
+ s = SignupPresenter.new(:user_login => nil)
89
+ s.valid?
90
+ message = s.errors.on(:user_login)
91
+
92
+ I18n.locale = old_locale
93
+
94
+ message
95
+ end
96
+
97
+ expect ['U53R pa22w0rD c4N n07 83 8L4nK'] do
98
+ old_locale = I18n.locale
99
+ I18n.locale = '1337'
100
+
101
+ s = SignupPresenter.new(:user_login => 'login')
102
+ s.valid?
103
+ message = s.errors.full_messages
104
+
105
+ I18n.locale = old_locale
106
+
107
+ message
108
+ end
109
+
68
110
  expect ActiveRecord::Base.to.receive(:transaction) do
69
111
  s = SignupPresenter.new
70
112
  s.save
@@ -115,6 +157,7 @@ Expectations do
115
157
  expect SignupPresenter.new.to.be.respond_to?(:user_login)
116
158
  expect SignupPresenter.new.to.be.respond_to?(:user_password_confirmation)
117
159
  expect SignupPresenter.new.to.be.respond_to?(:valid?) # just making sure i didn't break everything :)
160
+ expect SignupPresenter.new.to.be.respond_to?(:nil?, false) # making sure it's possible to pass 2 arguments
118
161
 
119
162
  expect User.create!(hash_for_user).not.to.be.login_changed? do |user|
120
163
  s = SignupPresenter.new(:user => user)
@@ -144,6 +187,11 @@ Expectations do
144
187
  })
145
188
  s.user_birthday
146
189
  end
190
+
191
+ expect nil do
192
+ s = SignupPresenter.new
193
+ s.attributes = nil
194
+ end
147
195
 
148
196
  # this is a regression test to make sure that _title is working. we had a weird conflict with using String#delete
149
197
  expect 'something' do
@@ -267,15 +315,15 @@ Expectations do
267
315
  end
268
316
 
269
317
  expect false do
270
- SignupNoNilPresenter.new.save
318
+ SignupNoAccountPresenter.new.save
271
319
  end
272
320
 
273
321
  expect true do
274
- SignupNoNilPresenter.new(:user => nil, :account => Account.new).save
322
+ SignupNoAccountPresenter.new(:user => User.new(hash_for_user), :account => nil).save
275
323
  end
276
324
 
277
325
  expect true do
278
- SignupNoNilPresenter.new(:user => nil, :account => Account.new).save!
326
+ SignupNoAccountPresenter.new(:user => User.new(hash_for_user), :account => nil).save!
279
327
  end
280
328
 
281
329
  expect Address do
data/test/test_helper.rb CHANGED
@@ -8,6 +8,21 @@ ActiveRecord::Base.establish_connection('sqlite3')
8
8
  ActiveRecord::Base.logger = Logger.new(STDERR)
9
9
  ActiveRecord::Base.logger.level = Logger::WARN
10
10
 
11
+ I18n.backend.store_translations '1337',
12
+ :activerecord => {
13
+ :models => {
14
+ :user => 'U53R'
15
+ },
16
+ :attributes => {
17
+ :user => {:password => 'pa22w0rD'}
18
+ },
19
+ :errors => {
20
+ :messages => {
21
+ :blank => 'c4N n07 83 8L4nK'
22
+ }
23
+ }
24
+ }
25
+
11
26
  ActiveRecord::Schema.define(:version => 0) do
12
27
  create_table :users do |t|
13
28
  t.boolean :admin, :default => false
@@ -31,9 +46,18 @@ ActiveRecord::Schema.define(:version => 0) do
31
46
  end
32
47
 
33
48
  class User < ActiveRecord::Base
34
- validates_presence_of :login, :password
49
+ validates_presence_of :login
50
+ validate :presence_of_password
35
51
  attr_accessible :login, :password, :birthday
36
52
  attr_accessor :password_confirmation
53
+
54
+ def presence_of_password
55
+ if password.blank?
56
+ attribute_name = I18n.t(:password, {:default => "Password", :scope => [:activerecord, :attributes, :user]})
57
+ error_message = I18n.t(:blank, {:default => "can't be blank", :scope => [:activerecord, :errors, :messages]})
58
+ errors.add_to_base("#{attribute_name} #{error_message}")
59
+ end
60
+ end
37
61
  end
38
62
  class Account < ActiveRecord::Base; end
39
63
  class Address < ActiveRecord::Base; end
@@ -59,11 +83,11 @@ class CantSavePresenter < ActivePresenter::Base
59
83
  def halt; false; end
60
84
  end
61
85
 
62
- class SignupNoNilPresenter < ActivePresenter::Base
86
+ class SignupNoAccountPresenter < ActivePresenter::Base
63
87
  presents :account, :user
64
88
 
65
89
  def save?(key, instance)
66
- !instance.nil?
90
+ key != :account
67
91
  end
68
92
  end
69
93
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_presenter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Golick & Daniel Haran
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-17 00:00:00 -04:00
12
+ date: 2009-12-17 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -25,17 +25,17 @@ files:
25
25
  - README
26
26
  - LICENSE
27
27
  - Rakefile
28
- - lib/active_presenter
29
28
  - lib/active_presenter/base.rb
30
29
  - lib/active_presenter/version.rb
31
30
  - lib/active_presenter.rb
32
- - lib/tasks
33
31
  - lib/tasks/doc.rake
34
32
  - lib/tasks/gem.rake
35
33
  - test/base_test.rb
36
34
  - test/test_helper.rb
37
35
  has_rdoc: true
38
36
  homepage: http://jamesgolick.com/active_presenter
37
+ licenses: []
38
+
39
39
  post_install_message:
40
40
  rdoc_options: []
41
41
 
@@ -56,9 +56,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
56
  requirements: []
57
57
 
58
58
  rubyforge_project: active_presenter
59
- rubygems_version: 1.3.1
59
+ rubygems_version: 1.3.5
60
60
  signing_key:
61
- specification_version: 2
61
+ specification_version: 3
62
62
  summary: ActivePresenter is the presenter library you already know! (...if you know ActiveRecord)
63
63
  test_files: []
64
64