public
Description: The presenter library you already know.
Homepage: http://jamesgolick.com/active_presenter
Clone URL: git://github.com/jamesgolick/active_presenter.git
active_presenter / test / test_helper.rb
100644 203 lines (156 sloc) 4.109 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
require File.dirname(__FILE__)+'/../lib/active_presenter'
require 'expectations'
require 'logger'
 
ActiveRecord::Base.configurations = {'sqlite3' => {:adapter => 'sqlite3', :database => ':memory:'}}
ActiveRecord::Base.establish_connection('sqlite3')
 
ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.logger.level = Logger::WARN
 
I18n.backend.store_translations '1337',
  :activerecord => {
    :models => {
      :user => 'U53R'
    },
    :attributes => {
      :user => {:password => 'pa22w0rD'}
    },
    :errors => {
      :messages => {
        :blank => 'c4N n07 83 8L4nK'
      }
    }
  }
  
ActiveRecord::Schema.define(:version => 0) do
  create_table :users do |t|
    t.boolean :admin, :default => false
    t.string :login, :default => ''
    t.string :password, :default => ''
    t.datetime :birthday
  end
  
  create_table :accounts do |t|
    t.string :subdomain, :default => ''
    t.string :title, :default => ''
  end
  
  create_table :addresses do |t|
    t.string :street
  end
 
  create_table :account_infos do |t|
    t.string :info
  end
end
 
class User < ActiveRecord::Base
  validates_presence_of :login
  validate :presence_of_password
  attr_accessible :login, :password, :birthday
  attr_accessor :password_confirmation
 
  def presence_of_password
    if password.blank?
      attribute_name = I18n.t(:password, {:default => "Password", :scope => [:activerecord, :attributes, :user]})
      error_message = I18n.t(:blank, {:default => "can't be blank", :scope => [:activerecord, :errors, :messages]})
      errors.add_to_base("#{attribute_name} #{error_message}")
    end
  end
end
class Account < ActiveRecord::Base; end
class Address < ActiveRecord::Base; end
class AccountInfo < ActiveRecord::Base; end
 
class PresenterWithTwoAddresses < ActivePresenter::Base
  presents :address, :secondary_address => Address
end
 
class SignupPresenter < ActivePresenter::Base
  presents :account, :user
end
 
class EndingWithSPresenter < ActivePresenter::Base
  presents :address
end
 
class CantSavePresenter < ActivePresenter::Base
  presents :address
  
  before_save :halt
  
  def halt; false; end
end
 
class SignupNoAccountPresenter < ActivePresenter::Base
  presents :account, :user
 
  def save?(key, instance)
    key != :account
  end
end
 
class AfterSavePresenter < ActivePresenter::Base
  presents :address
  
  after_save :set_street
  
  def set_street
    address.street = 'Some Street'
  end
end
 
class SamePrefixPresenter < ActivePresenter::Base
  presents :account, :account_info
end
 
class CallbackOrderingPresenter < ActivePresenter::Base
  presents :account
  
  before_validation :do_before_validation
  before_save :do_before_save
  after_save :do_after_save
  
  attr_reader :steps
  
  def initialize(params={})
    super
    @steps = []
  end
  
  def do_before_validation
    @steps << :before_validation
  end
  
  def do_before_save
    @steps << :before_save
  end
  
  def do_after_save
    @steps << :after_save
  end
end
 
class CallbackCantSavePresenter < ActivePresenter::Base
  presents :account
  
  before_validation :do_before_validation
  before_save :do_before_save
  before_save :halt
  after_save :do_after_save
  
  attr_reader :steps
  
  def initialize(params={})
    super
    @steps = []
  end
  
  def do_before_validation
    @steps << :before_validation
  end
  
  def do_before_save
    @steps << :before_save
  end
  
  def do_after_save
    @steps << :after_save
  end
 
  def halt
    false
  end
end
 
class CallbackCantValidatePresenter < ActivePresenter::Base
  presents :account
  
  before_validation :do_before_validation
  before_validation :halt
  before_save :do_before_save
  after_save :do_after_save
  
  attr_reader :steps
  
  def initialize(params={})
    super
    @steps = []
  end
  
  def do_before_validation
    @steps << :before_validation
  end
  
  def do_before_save
    @steps << :before_save
  end
  
  def do_after_save
    @steps << :after_save
  end
 
  def halt
    false
  end
end
 
def hash_for_user(opts = {})
  {:login => 'jane', :password => 'seekrit' }.merge(opts)
end