amrita2 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/README +1 -1
  2. data/init.rb +1 -0
  3. data/lib/amrita2/rails_bridge.rb +92 -7
  4. data/lib/amrita2/template.rb +12 -127
  5. data/lib/amrita2/testsupport.rb +0 -25
  6. data/lib/amrita2/version.rb +1 -1
  7. data/sample/depot/app/controllers/admin_controller.rb +3 -1
  8. data/sample/depot/app/helpers/cart_helper.rb +8 -0
  9. data/sample/depot/app/helpers/price_helper.rb +7 -0
  10. data/sample/depot/config/environment.rb +0 -14
  11. data/sample/depot/config/environments/development.rb +2 -2
  12. data/sample/depot/db/schema.rb +27 -20
  13. data/sample/depot/test/functional/admin_controller_test.rb +63 -22
  14. data/sample/depot/test/functional/store_controller_test.rb +30 -18
  15. data/sample/depot/vendor/plugins/will_paginate/init.rb +4 -0
  16. data/sample/depot/vendor/plugins/will_paginate/lib/will_paginate.rb +61 -0
  17. data/sample/depot/vendor/plugins/will_paginate/lib/will_paginate/collection.rb +132 -0
  18. data/sample/depot/vendor/plugins/will_paginate/lib/will_paginate/core_ext.rb +80 -0
  19. data/sample/depot/vendor/plugins/will_paginate/lib/will_paginate/finder.rb +181 -0
  20. data/sample/depot/vendor/plugins/will_paginate/lib/will_paginate/view_helpers.rb +206 -0
  21. data/sample/depot/vendor/plugins/will_paginate/test/array_pagination_test.rb +131 -0
  22. data/sample/depot/vendor/plugins/will_paginate/test/boot.rb +23 -0
  23. data/sample/depot/vendor/plugins/will_paginate/test/finder_test.rb +290 -0
  24. data/sample/depot/vendor/plugins/will_paginate/test/fixtures/admin.rb +3 -0
  25. data/sample/depot/vendor/plugins/will_paginate/test/fixtures/company.rb +9 -0
  26. data/sample/depot/vendor/plugins/will_paginate/test/fixtures/developer.rb +11 -0
  27. data/sample/depot/vendor/plugins/will_paginate/test/fixtures/project.rb +15 -0
  28. data/sample/depot/vendor/plugins/will_paginate/test/fixtures/reply.rb +5 -0
  29. data/sample/depot/vendor/plugins/will_paginate/test/fixtures/schema.rb +38 -0
  30. data/sample/depot/vendor/plugins/will_paginate/test/fixtures/topic.rb +4 -0
  31. data/sample/depot/vendor/plugins/will_paginate/test/fixtures/user.rb +2 -0
  32. data/sample/depot/vendor/plugins/will_paginate/test/helper.rb +25 -0
  33. data/sample/depot/vendor/plugins/will_paginate/test/lib/activerecord_test_case.rb +23 -0
  34. data/sample/depot/vendor/plugins/will_paginate/test/lib/activerecord_test_connector.rb +67 -0
  35. data/sample/depot/vendor/plugins/will_paginate/test/lib/load_fixtures.rb +13 -0
  36. data/sample/depot/vendor/plugins/will_paginate/test/pagination_test.rb +240 -0
  37. data/sample/hello/test1.rb +23 -0
  38. data/sample/login_engine/config/environment.rb +18 -20
  39. data/sample/login_engine/config/environments/development.rb +2 -2
  40. data/sample/login_engine/db/schema.rb +24 -17
  41. data/sample/login_engine/lib/login_engine/authenticated_system.rb +18 -18
  42. data/sample/login_engine/test/functional/user_controller_test.rb +1 -0
  43. data/sample/ramaze/ramaise_amrita2.rb +156 -0
  44. data/specs/attribute.rb +11 -0
  45. data/specs/datatypes.rb +13 -0
  46. data/specs/sample.rb +1 -2
  47. data/specs/sanitize.rb +14 -0
  48. metadata +28 -7
  49. data/sample/depot/app/helpers/ar_form.rb +0 -169
  50. data/sample/depot/app/helpers/form_tag.rb +0 -24
  51. data/sample/depot/app/helpers/standard_form.rb +0 -73
  52. data/sample/depot/test/integration/dsl_user_stories_test.rb +0 -126
  53. data/sample/depot/test/integration/user_stories_test.rb +0 -70
@@ -0,0 +1,131 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+ require 'will_paginate/core_ext'
3
+
4
+ class ArrayPaginationTest < Test::Unit::TestCase
5
+ def test_simple
6
+ collection = ('a'..'e').to_a
7
+
8
+ [{ :page => 1, :per_page => 3, :expected => %w( a b c ) },
9
+ { :page => 2, :per_page => 3, :expected => %w( d e ) },
10
+ { :page => 1, :per_page => 5, :expected => %w( a b c d e ) },
11
+ { :page => 3, :per_page => 5, :expected => [] },
12
+ ].
13
+ each do |conditions|
14
+ assert_equal conditions[:expected], collection.paginate(conditions.slice(:page, :per_page))
15
+ end
16
+ end
17
+
18
+ def test_defaults
19
+ result = (1..50).to_a.paginate
20
+ assert_equal 1, result.current_page
21
+ assert_equal 30, result.size
22
+ end
23
+
24
+ def test_deprecated_api
25
+ assert_deprecated 'paginate API' do
26
+ result = (1..50).to_a.paginate(2, 10)
27
+ assert_equal 2, result.current_page
28
+ assert_equal (11..20).to_a, result
29
+ assert_equal 50, result.total_entries
30
+ end
31
+
32
+ assert_deprecated { [].paginate nil }
33
+ end
34
+
35
+ def test_total_entries_has_precedence
36
+ result = %w(a b c).paginate :total_entries => 5
37
+ assert_equal 5, result.total_entries
38
+ end
39
+
40
+ def test_argument_error_with_params_and_another_argument
41
+ assert_raise ArgumentError do
42
+ [].paginate({}, 5)
43
+ end
44
+ end
45
+
46
+ def test_paginated_collection
47
+ entries = %w(a b c)
48
+ collection = create(2, 3, 10) do |pager|
49
+ assert_equal entries, pager.replace(entries)
50
+ end
51
+
52
+ assert_equal entries, collection
53
+ assert_respond_to_all collection, %w(page_count each offset size current_page per_page total_entries)
54
+ assert_kind_of Array, collection
55
+ assert_instance_of Array, collection.entries
56
+ assert_equal 3, collection.offset
57
+ assert_equal 4, collection.page_count
58
+ assert !collection.out_of_bounds?
59
+ end
60
+
61
+ def test_out_of_bounds
62
+ entries = create(2, 3, 2){}
63
+ assert entries.out_of_bounds?
64
+
65
+ entries = create(1, 3, 2){}
66
+ assert !entries.out_of_bounds?
67
+ end
68
+
69
+ def test_guessing_total_count
70
+ entries = create do |pager|
71
+ # collection is shorter than limit
72
+ pager.replace array
73
+ end
74
+ assert_equal 8, entries.total_entries
75
+
76
+ entries = create(2, 5, 10) do |pager|
77
+ # collection is shorter than limit, but we have an explicit count
78
+ pager.replace array
79
+ end
80
+ assert_equal 10, entries.total_entries
81
+
82
+ entries = create do |pager|
83
+ # collection is the same as limit; we can't guess
84
+ pager.replace array(5)
85
+ end
86
+ assert_equal nil, entries.total_entries
87
+
88
+ entries = create do |pager|
89
+ # collection is empty; we can't guess
90
+ pager.replace array(0)
91
+ end
92
+ assert_equal nil, entries.total_entries
93
+ end
94
+
95
+ def test_invalid_page
96
+ bad_input = [0, -1, nil, '', 'Schnitzel']
97
+
98
+ bad_input.each do |bad|
99
+ assert_raise(WillPaginate::InvalidPage) { create(bad) }
100
+ end
101
+ end
102
+
103
+ def test_invalid_per_page_setting
104
+ assert_raise(ArgumentError) { create(1, -1) }
105
+ end
106
+
107
+ private
108
+ def create(page = 2, limit = 5, total = nil, &block)
109
+ if block_given?
110
+ WillPaginate::Collection.create(page, limit, total, &block)
111
+ else
112
+ WillPaginate::Collection.new(page, limit, total)
113
+ end
114
+ end
115
+
116
+ def array(size = 3)
117
+ Array.new(size)
118
+ end
119
+
120
+ def collect_deprecations
121
+ old_behavior = WillPaginate::Deprecation.behavior
122
+ deprecations = []
123
+ WillPaginate::Deprecation.behavior = Proc.new do |message, callstack|
124
+ deprecations << message
125
+ end
126
+ result = yield
127
+ [result, deprecations]
128
+ ensure
129
+ WillPaginate::Deprecation.behavior = old_behavior
130
+ end
131
+ end
@@ -0,0 +1,23 @@
1
+ plugin_root = File.join(File.dirname(__FILE__), '..')
2
+ version = ENV['RAILS_VERSION']
3
+ version = nil if version and version == ""
4
+
5
+ # first look for a symlink to a copy of the framework
6
+ if !version and framework_root = ["#{plugin_root}/rails", "#{plugin_root}/../../rails"].find { |p| File.directory? p }
7
+ puts "found framework root: #{framework_root}"
8
+ # this allows for a plugin to be tested outside of an app and without Rails gems
9
+ $:.unshift "#{framework_root}/activesupport/lib", "#{framework_root}/activerecord/lib", "#{framework_root}/actionpack/lib"
10
+ else
11
+ # simply use installed gems if available
12
+ puts "using Rails#{version ? ' ' + version : nil} gems"
13
+ require 'rubygems'
14
+
15
+ if version
16
+ gem 'rails', version
17
+ else
18
+ gem 'actionpack'
19
+ gem 'activerecord'
20
+ end
21
+ end
22
+
23
+ $:.unshift "#{plugin_root}/lib"
@@ -0,0 +1,290 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+ require File.dirname(__FILE__) + '/lib/activerecord_test_case'
3
+
4
+ require 'will_paginate'
5
+ WillPaginate.enable_activerecord
6
+
7
+ class FinderTest < ActiveRecordTestCase
8
+ fixtures :topics, :replies, :users, :projects, :developers_projects
9
+
10
+ def test_new_methods_presence
11
+ assert_respond_to_all Topic, %w(per_page paginate paginate_by_sql)
12
+ end
13
+
14
+ def test_simple_paginate
15
+ entries = Topic.paginate :page => nil
16
+ assert_equal 1, entries.current_page
17
+ assert_nil entries.previous_page
18
+ assert_nil entries.next_page
19
+ assert_equal 1, entries.page_count
20
+ assert_equal 4, entries.size
21
+
22
+ entries = Topic.paginate :page => 2
23
+ assert_equal 2, entries.current_page
24
+ assert_equal 1, entries.previous_page
25
+ assert_equal 1, entries.page_count
26
+ assert entries.empty?
27
+ end
28
+
29
+ def test_parameter_api
30
+ # :page parameter in options is required!
31
+ assert_raise(ArgumentError){ Topic.paginate }
32
+ assert_raise(ArgumentError){ Topic.paginate({}) }
33
+
34
+ # explicit :all should not break anything
35
+ assert_equal Topic.paginate(:page => nil), Topic.paginate(:all, :page => 1)
36
+
37
+ # :count could be nil and we should still not cry
38
+ assert_nothing_raised { Topic.paginate :page => 1, :count => nil }
39
+ end
40
+
41
+ def test_paginate_with_per_page
42
+ entries = Topic.paginate :page => 1, :per_page => 1
43
+ assert_equal 1, entries.size
44
+ assert_equal 4, entries.page_count
45
+
46
+ # Developer class has explicit per_page at 10
47
+ entries = Developer.paginate :page => 1
48
+ assert_equal 10, entries.size
49
+ assert_equal 2, entries.page_count
50
+
51
+ entries = Developer.paginate :page => 1, :per_page => 5
52
+ assert_equal 11, entries.total_entries
53
+ assert_equal 5, entries.size
54
+ assert_equal 3, entries.page_count
55
+ end
56
+
57
+ def test_paginate_with_order
58
+ entries = Topic.paginate :page => 1, :order => 'created_at desc'
59
+ expected = [topics(:futurama), topics(:harvey_birdman), topics(:rails), topics(:ar)].reverse
60
+ assert_equal expected, entries.to_a
61
+ assert_equal 1, entries.page_count
62
+ end
63
+
64
+ def test_paginate_with_conditions
65
+ entries = Topic.paginate :page => 1, :conditions => ["created_at > ?", 30.minutes.ago]
66
+ expected = [topics(:rails), topics(:ar)]
67
+ assert_equal expected, entries.to_a
68
+ assert_equal 1, entries.page_count
69
+ end
70
+
71
+ def test_paginate_with_include_and_conditions
72
+ entries = Topic.paginate \
73
+ :page => 1,
74
+ :include => :replies,
75
+ :conditions => "replies.content LIKE 'Bird%' ",
76
+ :per_page => 10
77
+
78
+ expected = Topic.find :all,
79
+ :include => 'replies',
80
+ :conditions => "replies.content LIKE 'Bird%' ",
81
+ :limit => 10
82
+
83
+ assert_equal expected, entries.to_a
84
+ assert_equal 1, entries.total_entries
85
+ end
86
+
87
+ def test_paginate_with_include_and_order
88
+ entries = Topic.paginate \
89
+ :page => 1,
90
+ :include => :replies,
91
+ :order => 'replies.created_at asc, topics.created_at asc',
92
+ :per_page => 10
93
+
94
+ expected = Topic.find :all,
95
+ :include => 'replies',
96
+ :order => 'replies.created_at asc, topics.created_at asc',
97
+ :limit => 10
98
+
99
+ assert_equal expected, entries.to_a
100
+ assert_equal 4, entries.total_entries
101
+ end
102
+
103
+ def test_paginate_associations_with_include
104
+ entries, project = nil, projects(:active_record)
105
+
106
+ assert_nothing_raised "THIS IS A BUG in Rails 1.2.3 that was fixed in [7326]. " +
107
+ "Please upgrade to the 1-2-stable branch or edge Rails." do
108
+ entries = project.topics.paginate \
109
+ :page => 1,
110
+ :include => :replies,
111
+ :conditions => "replies.content LIKE 'Nice%' ",
112
+ :per_page => 10
113
+ end
114
+
115
+ expected = Topic.find :all,
116
+ :include => 'replies',
117
+ :conditions => "project_id = #{project.id} AND replies.content LIKE 'Nice%' ",
118
+ :limit => 10
119
+
120
+ assert_equal expected, entries.to_a
121
+ end
122
+
123
+ def test_paginate_associations
124
+ dhh = users :david
125
+ expected_name_ordered = [projects(:action_controller), projects(:active_record)]
126
+ expected_id_ordered = [projects(:active_record), projects(:action_controller)]
127
+
128
+ # with association-specified order
129
+ entries = dhh.projects.paginate(:page => 1)
130
+ assert_equal expected_name_ordered, entries
131
+ assert_equal 2, entries.total_entries
132
+
133
+ # with explicit order
134
+ entries = dhh.projects.paginate(:page => 1, :order => 'projects.id')
135
+ assert_equal expected_id_ordered, entries
136
+ assert_equal 2, entries.total_entries
137
+
138
+ assert_nothing_raised { dhh.projects.find(:all, :order => 'projects.id', :limit => 4) }
139
+ entries = dhh.projects.paginate(:page => 1, :order => 'projects.id', :per_page => 4)
140
+ assert_equal expected_id_ordered, entries
141
+
142
+ # has_many with implicit order
143
+ topic = Topic.find(1)
144
+ expected = [replies(:spam), replies(:witty_retort)]
145
+ assert_equal expected.map(&:id).sort, topic.replies.paginate(:page => 1).map(&:id).sort
146
+ assert_equal expected.reverse, topic.replies.paginate(:page => 1, :order => 'replies.id ASC')
147
+ end
148
+
149
+ def test_paginate_association_extension
150
+ project = Project.find(:first)
151
+ entries = project.replies.paginate_recent :page => 1
152
+ assert_equal [replies(:brave)], entries
153
+ end
154
+
155
+ def test_paginate_with_joins
156
+ entries = Developer.paginate :page => 1,
157
+ :joins => 'LEFT JOIN developers_projects ON users.id = developers_projects.developer_id',
158
+ :conditions => 'project_id = 1'
159
+ assert_equal 2, entries.size
160
+ developer_names = entries.map { |d| d.name }
161
+ assert developer_names.include?('David')
162
+ assert developer_names.include?('Jamis')
163
+
164
+ expected = entries.to_a
165
+ entries = Developer.paginate :page => 1,
166
+ :joins => 'LEFT JOIN developers_projects ON users.id = developers_projects.developer_id',
167
+ :conditions => 'project_id = 1', :count => { :select => "users.id" }
168
+ assert_equal expected, entries.to_a
169
+ end
170
+
171
+ def test_paginate_with_group
172
+ entries = Developer.paginate :page => 1, :per_page => 10, :group => 'salary'
173
+ expected = [ users(:david), users(:jamis), users(:dev_10), users(:poor_jamis) ].map(&:salary).sort
174
+ assert_equal expected, entries.map(&:salary).sort
175
+ end
176
+
177
+ def test_paginate_with_dynamic_finder
178
+ expected = [replies(:witty_retort), replies(:spam)]
179
+ assert_equal expected, Reply.paginate_by_topic_id(1, :page => 1)
180
+
181
+ entries = Developer.paginate :conditions => { :salary => 100000 }, :page => 1, :per_page => 5
182
+ assert_equal 8, entries.total_entries
183
+ assert_equal entries, Developer.paginate_by_salary(100000, :page => 1, :per_page => 5)
184
+
185
+ # dynamic finder + conditions
186
+ entries = Developer.paginate_by_salary(100000, :page => 1,
187
+ :conditions => ['id > ?', 6])
188
+ assert_equal 4, entries.total_entries
189
+ assert_equal (7..10).to_a, entries.map(&:id)
190
+
191
+ assert_raises NoMethodError do
192
+ Developer.paginate_by_inexistent_attribute 100000, :page => 1
193
+ end
194
+ end
195
+
196
+ def test_count_distinct
197
+ entries = Developer.paginate :select => 'DISTINCT salary', :page => 1, :per_page => 4
198
+ assert_equal 4, entries.size
199
+ assert_equal 4, entries.total_entries
200
+ end
201
+
202
+ def test_scoped_paginate
203
+ entries = Developer.with_poor_ones { Developer.paginate :page => 1 }
204
+
205
+ assert_equal 2, entries.size
206
+ assert_equal 2, entries.total_entries
207
+ end
208
+
209
+ # Are we on edge? Find out by testing find_all which was removed in [6998]
210
+ unless Developer.respond_to? :find_all
211
+ def test_paginate_array_of_ids
212
+ # AR finders also accept arrays of IDs
213
+ # (this was broken in Rails before [6912])
214
+ entries = Developer.paginate((1..8).to_a, :per_page => 3, :page => 2)
215
+ assert_equal (4..6).to_a, entries.map(&:id)
216
+ assert_equal 8, entries.total_entries
217
+ end
218
+ end
219
+
220
+ uses_mocha 'internals' do
221
+ def test_implicit_all_with_dynamic_finders
222
+ Topic.expects(:find_all_by_foo).returns([])
223
+ Topic.expects(:wp_extract_finder_conditions)
224
+ Topic.expects(:count)
225
+ Topic.paginate_by_foo :page => 1
226
+ end
227
+
228
+ def test_guessing_the_total_count
229
+ Topic.expects(:find).returns(Array.new(2))
230
+ Topic.expects(:count).never
231
+
232
+ entries = Topic.paginate :page => 2, :per_page => 4
233
+ assert_equal 6, entries.total_entries
234
+ end
235
+
236
+ def test_extra_parameters_stay_untouched
237
+ Topic.expects(:find).with() { |*args| args.last.key? :foo }.returns(Array.new(5))
238
+ Topic.expects(:count).with(){ |*args| args.last.key? :foo }.returns(1)
239
+
240
+ Topic.paginate :foo => 'bar', :page => 1, :per_page => 4
241
+ end
242
+
243
+ def test_count_doesnt_use_select_options
244
+ Developer.expects(:find).with() { |*args| args.last.key? :select }.returns(Array.new(5))
245
+ Developer.expects(:count).with(){ |*args| !args.last.key? :select }.returns(1)
246
+
247
+ Developer.paginate :select => 'users.*', :page => 1, :per_page => 4
248
+ end
249
+
250
+ def test_should_use_scoped_finders_if_present
251
+ # scope-out compatibility
252
+ Topic.expects(:find_best).returns(Array.new(5))
253
+ Topic.expects(:with_best).returns(1)
254
+
255
+ Topic.paginate_best :page => 1, :per_page => 4
256
+ end
257
+
258
+ def test_ability_to_use_with_custom_finders
259
+ # acts_as_taggable defines `find_tagged_with(tag, options)`
260
+ Topic.expects(:find_tagged_with).with('will_paginate', :offset => 0, :limit => 5).returns([])
261
+ Topic.expects(:count).with({}).returns(0)
262
+
263
+ Topic.paginate_tagged_with 'will_paginate', :page => 1, :per_page => 5
264
+ end
265
+
266
+ def test_paginate_by_sql
267
+ assert_respond_to Developer, :paginate_by_sql
268
+ Developer.expects(:find_by_sql).with('sql LIMIT 3 OFFSET 3').returns([])
269
+ Developer.expects(:count_by_sql).with('SELECT COUNT(*) FROM (sql) AS count_table').returns(0)
270
+
271
+ entries = Developer.paginate_by_sql 'sql', :page => 2, :per_page => 3
272
+ assert_equal 0, entries.total_entries
273
+ end
274
+
275
+ def test_paginate_by_sql_respects_total_entries_setting
276
+ Developer.expects(:find_by_sql).returns([])
277
+ Developer.expects(:count_by_sql).never
278
+
279
+ entries = Developer.paginate_by_sql 'sql', :page => 1, :total_entries => 999
280
+ assert_equal 999, entries.total_entries
281
+ end
282
+
283
+ def test_paginate_by_sql_strips_order_by_when_counting
284
+ Developer.expects(:find_by_sql).returns([])
285
+ Developer.expects(:count_by_sql).with("SELECT COUNT(*) FROM (sql\n ) AS count_table").returns(0)
286
+
287
+ entries = Developer.paginate_by_sql "sql\n ORDER\nby foo, bar, `baz` ASC", :page => 1
288
+ end
289
+ end
290
+ end
@@ -0,0 +1,3 @@
1
+ class Admin < User
2
+ has_many :companies, :finder_sql => 'SELECT * FROM companies'
3
+ end