globe-composite_primary_keys 3.0.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.
Files changed (150) hide show
  1. data/History.txt +203 -0
  2. data/Manifest.txt +121 -0
  3. data/README.txt +41 -0
  4. data/README_DB2.txt +33 -0
  5. data/Rakefile +30 -0
  6. data/composite_primary_keys.gemspec +17 -0
  7. data/lib/adapter_helper/base.rb +63 -0
  8. data/lib/adapter_helper/mysql.rb +13 -0
  9. data/lib/adapter_helper/oracle.rb +12 -0
  10. data/lib/adapter_helper/oracle_enhanced.rb +12 -0
  11. data/lib/adapter_helper/postgresql.rb +13 -0
  12. data/lib/adapter_helper/sqlite3.rb +13 -0
  13. data/lib/composite_primary_keys.rb +63 -0
  14. data/lib/composite_primary_keys/association_preload.rb +162 -0
  15. data/lib/composite_primary_keys/associations.rb +159 -0
  16. data/lib/composite_primary_keys/attribute_methods.rb +84 -0
  17. data/lib/composite_primary_keys/base.rb +200 -0
  18. data/lib/composite_primary_keys/composite_arrays.rb +29 -0
  19. data/lib/composite_primary_keys/connection_adapters/abstract_adapter.rb +9 -0
  20. data/lib/composite_primary_keys/connection_adapters/ibm_db_adapter.rb +21 -0
  21. data/lib/composite_primary_keys/connection_adapters/oracle_adapter.rb +15 -0
  22. data/lib/composite_primary_keys/connection_adapters/oracle_enhanced_adapter.rb +17 -0
  23. data/lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb +53 -0
  24. data/lib/composite_primary_keys/connection_adapters/sqlite3_adapter.rb +15 -0
  25. data/lib/composite_primary_keys/finder_methods.rb +68 -0
  26. data/lib/composite_primary_keys/fixtures.rb +8 -0
  27. data/lib/composite_primary_keys/read.rb +25 -0
  28. data/lib/composite_primary_keys/reflection.rb +39 -0
  29. data/lib/composite_primary_keys/relation.rb +31 -0
  30. data/lib/composite_primary_keys/through_association_scope.rb +212 -0
  31. data/lib/composite_primary_keys/validations/uniqueness.rb +118 -0
  32. data/lib/composite_primary_keys/version.rb +9 -0
  33. data/loader.rb +24 -0
  34. data/local/database_connections.rb.sample +12 -0
  35. data/local/paths.rb.sample +2 -0
  36. data/local/tasks.rb.sample +2 -0
  37. data/scripts/console.rb +48 -0
  38. data/scripts/txt2html +67 -0
  39. data/scripts/txt2js +59 -0
  40. data/tasks/activerecord_selection.rake +43 -0
  41. data/tasks/databases.rake +12 -0
  42. data/tasks/databases/mysql.rake +30 -0
  43. data/tasks/databases/oracle.rake +25 -0
  44. data/tasks/databases/postgresql.rake +25 -0
  45. data/tasks/databases/sqlite3.rake +28 -0
  46. data/tasks/deployment.rake +22 -0
  47. data/tasks/local_setup.rake +13 -0
  48. data/tasks/website.rake +18 -0
  49. data/test/README_tests.txt +67 -0
  50. data/test/abstract_unit.rb +103 -0
  51. data/test/connections/native_ibm_db/connection.rb +23 -0
  52. data/test/connections/native_mysql/connection.rb +13 -0
  53. data/test/connections/native_oracle/connection.rb +14 -0
  54. data/test/connections/native_oracle_enhanced/connection.rb +20 -0
  55. data/test/connections/native_postgresql/connection.rb +8 -0
  56. data/test/connections/native_sqlite/connection.rb +9 -0
  57. data/test/fixtures/article.rb +5 -0
  58. data/test/fixtures/article_group.rb +4 -0
  59. data/test/fixtures/article_groups.yml +7 -0
  60. data/test/fixtures/articles.yml +6 -0
  61. data/test/fixtures/comment.rb +6 -0
  62. data/test/fixtures/comments.yml +16 -0
  63. data/test/fixtures/db_definitions/db2-create-tables.sql +113 -0
  64. data/test/fixtures/db_definitions/db2-drop-tables.sql +16 -0
  65. data/test/fixtures/db_definitions/mysql.sql +181 -0
  66. data/test/fixtures/db_definitions/oracle.drop.sql +39 -0
  67. data/test/fixtures/db_definitions/oracle.sql +188 -0
  68. data/test/fixtures/db_definitions/postgresql.sql +206 -0
  69. data/test/fixtures/db_definitions/sqlite.sql +166 -0
  70. data/test/fixtures/department.rb +5 -0
  71. data/test/fixtures/departments.yml +3 -0
  72. data/test/fixtures/dorm.rb +3 -0
  73. data/test/fixtures/dorms.yml +2 -0
  74. data/test/fixtures/employee.rb +4 -0
  75. data/test/fixtures/employees.yml +9 -0
  76. data/test/fixtures/group.rb +3 -0
  77. data/test/fixtures/groups.yml +3 -0
  78. data/test/fixtures/hack.rb +6 -0
  79. data/test/fixtures/hacks.yml +2 -0
  80. data/test/fixtures/kitchen_sink.rb +3 -0
  81. data/test/fixtures/kitchen_sinks.yml +5 -0
  82. data/test/fixtures/membership.rb +10 -0
  83. data/test/fixtures/membership_status.rb +3 -0
  84. data/test/fixtures/membership_statuses.yml +10 -0
  85. data/test/fixtures/memberships.yml +6 -0
  86. data/test/fixtures/product.rb +7 -0
  87. data/test/fixtures/product_tariff.rb +5 -0
  88. data/test/fixtures/product_tariffs.yml +12 -0
  89. data/test/fixtures/products.yml +6 -0
  90. data/test/fixtures/reading.rb +4 -0
  91. data/test/fixtures/readings.yml +10 -0
  92. data/test/fixtures/reference_code.rb +7 -0
  93. data/test/fixtures/reference_codes.yml +28 -0
  94. data/test/fixtures/reference_type.rb +7 -0
  95. data/test/fixtures/reference_types.yml +9 -0
  96. data/test/fixtures/restaurant.rb +6 -0
  97. data/test/fixtures/restaurants.yml +5 -0
  98. data/test/fixtures/restaurants_suburbs.yml +11 -0
  99. data/test/fixtures/room.rb +10 -0
  100. data/test/fixtures/room_assignment.rb +4 -0
  101. data/test/fixtures/room_assignments.yml +4 -0
  102. data/test/fixtures/room_attribute.rb +3 -0
  103. data/test/fixtures/room_attribute_assignment.rb +5 -0
  104. data/test/fixtures/room_attribute_assignments.yml +4 -0
  105. data/test/fixtures/room_attributes.yml +3 -0
  106. data/test/fixtures/rooms.yml +3 -0
  107. data/test/fixtures/seat.rb +5 -0
  108. data/test/fixtures/seats.yml +4 -0
  109. data/test/fixtures/street.rb +3 -0
  110. data/test/fixtures/streets.yml +15 -0
  111. data/test/fixtures/student.rb +4 -0
  112. data/test/fixtures/students.yml +2 -0
  113. data/test/fixtures/suburb.rb +6 -0
  114. data/test/fixtures/suburbs.yml +9 -0
  115. data/test/fixtures/tariff.rb +6 -0
  116. data/test/fixtures/tariffs.yml +13 -0
  117. data/test/fixtures/user.rb +10 -0
  118. data/test/fixtures/users.yml +6 -0
  119. data/test/hash_tricks.rb +34 -0
  120. data/test/plugins/pagination.rb +405 -0
  121. data/test/plugins/pagination_helper.rb +135 -0
  122. data/test/test_associations.rb +178 -0
  123. data/test/test_attribute_methods.rb +22 -0
  124. data/test/test_attributes.rb +80 -0
  125. data/test/test_clone.rb +34 -0
  126. data/test/test_composite_arrays.rb +32 -0
  127. data/test/test_create.rb +68 -0
  128. data/test/test_delete.rb +83 -0
  129. data/test/test_exists.rb +25 -0
  130. data/test/test_find.rb +73 -0
  131. data/test/test_ids.rb +90 -0
  132. data/test/test_miscellaneous.rb +39 -0
  133. data/test/test_pagination.rb +38 -0
  134. data/test/test_polymorphic.rb +32 -0
  135. data/test/test_santiago.rb +27 -0
  136. data/test/test_suite.rb +19 -0
  137. data/test/test_tutorial_example.rb +26 -0
  138. data/test/test_update.rb +40 -0
  139. data/test/test_validations.rb +11 -0
  140. data/website/index.html +195 -0
  141. data/website/index.txt +159 -0
  142. data/website/javascripts/rounded_corners_lite.inc.js +285 -0
  143. data/website/stylesheets/screen.css +126 -0
  144. data/website/template.js +3 -0
  145. data/website/template.rhtml +53 -0
  146. data/website/version-raw.js +3 -0
  147. data/website/version-raw.txt +2 -0
  148. data/website/version.js +4 -0
  149. data/website/version.txt +3 -0
  150. metadata +339 -0
@@ -0,0 +1,135 @@
1
+ module ActionView
2
+ module Helpers
3
+ # Provides methods for linking to ActionController::Pagination objects using a simple generator API. You can optionally
4
+ # also build your links manually using ActionView::Helpers::AssetHelper#link_to like so:
5
+ #
6
+ # <%= link_to "Previous page", { :page => paginator.current.previous } if paginator.current.previous %>
7
+ # <%= link_to "Next page", { :page => paginator.current.next } if paginator.current.next %>
8
+ module PaginationHelper
9
+ unless const_defined?(:DEFAULT_OPTIONS)
10
+ DEFAULT_OPTIONS = {
11
+ :name => :page,
12
+ :window_size => 2,
13
+ :always_show_anchors => true,
14
+ :link_to_current_page => false,
15
+ :params => {}
16
+ }
17
+ end
18
+
19
+ # Creates a basic HTML link bar for the given +paginator+. Links will be created
20
+ # for the next and/or previous page and for a number of other pages around the current
21
+ # pages position. The +html_options+ hash is passed to +link_to+ when the links are created.
22
+ #
23
+ # ==== Options
24
+ # <tt>:name</tt>:: the routing name for this paginator
25
+ # (defaults to +page+)
26
+ # <tt>:prefix</tt>:: prefix for pagination links
27
+ # (i.e. Older Pages: 1 2 3 4)
28
+ # <tt>:suffix</tt>:: suffix for pagination links
29
+ # (i.e. 1 2 3 4 <- Older Pages)
30
+ # <tt>:window_size</tt>:: the number of pages to show around
31
+ # the current page (defaults to <tt>2</tt>)
32
+ # <tt>:always_show_anchors</tt>:: whether or not the first and last
33
+ # pages should always be shown
34
+ # (defaults to +true+)
35
+ # <tt>:link_to_current_page</tt>:: whether or not the current page
36
+ # should be linked to (defaults to
37
+ # +false+)
38
+ # <tt>:params</tt>:: any additional routing parameters
39
+ # for page URLs
40
+ #
41
+ # ==== Examples
42
+ # # We'll assume we have a paginator setup in @person_pages...
43
+ #
44
+ # pagination_links(@person_pages)
45
+ # # => 1 <a href="/?page=2/">2</a> <a href="/?page=3/">3</a> ... <a href="/?page=10/">10</a>
46
+ #
47
+ # pagination_links(@person_pages, :link_to_current_page => true)
48
+ # # => <a href="/?page=1/">1</a> <a href="/?page=2/">2</a> <a href="/?page=3/">3</a> ... <a href="/?page=10/">10</a>
49
+ #
50
+ # pagination_links(@person_pages, :always_show_anchors => false)
51
+ # # => 1 <a href="/?page=2/">2</a> <a href="/?page=3/">3</a>
52
+ #
53
+ # pagination_links(@person_pages, :window_size => 1)
54
+ # # => 1 <a href="/?page=2/">2</a> ... <a href="/?page=10/">10</a>
55
+ #
56
+ # pagination_links(@person_pages, :params => { :viewer => "flash" })
57
+ # # => 1 <a href="/?page=2&amp;viewer=flash/">2</a> <a href="/?page=3&amp;viewer=flash/">3</a> ...
58
+ # # <a href="/?page=10&amp;viewer=flash/">10</a>
59
+ def pagination_links(paginator, options={}, html_options={})
60
+ name = options[:name] || DEFAULT_OPTIONS[:name]
61
+ params = (options[:params] || DEFAULT_OPTIONS[:params]).clone
62
+
63
+ prefix = options[:prefix] || ''
64
+ suffix = options[:suffix] || ''
65
+
66
+ pagination_links_each(paginator, options, prefix, suffix) do |n|
67
+ params[name] = n
68
+ link_to(n.to_s, params, html_options)
69
+ end
70
+ end
71
+
72
+ # Iterate through the pages of a given +paginator+, invoking a
73
+ # block for each page number that needs to be rendered as a link.
74
+ #
75
+ # ==== Options
76
+ # <tt>:window_size</tt>:: the number of pages to show around
77
+ # the current page (defaults to +2+)
78
+ # <tt>:always_show_anchors</tt>:: whether or not the first and last
79
+ # pages should always be shown
80
+ # (defaults to +true+)
81
+ # <tt>:link_to_current_page</tt>:: whether or not the current page
82
+ # should be linked to (defaults to
83
+ # +false+)
84
+ #
85
+ # ==== Example
86
+ # # Turn paginated links into an Ajax call
87
+ # pagination_links_each(paginator, page_options) do |link|
88
+ # options = { :url => {:action => 'list'}, :update => 'results' }
89
+ # html_options = { :href => url_for(:action => 'list') }
90
+ #
91
+ # link_to_remote(link.to_s, options, html_options)
92
+ # end
93
+ def pagination_links_each(paginator, options, prefix = nil, suffix = nil)
94
+ options = DEFAULT_OPTIONS.merge(options)
95
+ link_to_current_page = options[:link_to_current_page]
96
+ always_show_anchors = options[:always_show_anchors]
97
+
98
+ current_page = paginator.current_page
99
+ window_pages = current_page.window(options[:window_size]).pages
100
+ return if window_pages.length <= 1 unless link_to_current_page
101
+
102
+ first, last = paginator.first, paginator.last
103
+
104
+ html = ''
105
+
106
+ html << prefix if prefix
107
+
108
+ if always_show_anchors and not (wp_first = window_pages[0]).first?
109
+ html << yield(first.number)
110
+ html << ' ... ' if wp_first.number - first.number > 1
111
+ html << ' '
112
+ end
113
+
114
+ window_pages.each do |page|
115
+ if current_page == page && !link_to_current_page
116
+ html << page.number.to_s
117
+ else
118
+ html << yield(page.number)
119
+ end
120
+ html << ' '
121
+ end
122
+
123
+ if always_show_anchors and not (wp_last = window_pages[-1]).last?
124
+ html << ' ... ' if last.number - wp_last.number > 1
125
+ html << yield(last.number)
126
+ end
127
+
128
+ html << suffix if suffix
129
+
130
+ html
131
+ end
132
+
133
+ end # PaginationHelper
134
+ end # Helpers
135
+ end # ActionView
@@ -0,0 +1,178 @@
1
+ require 'abstract_unit'
2
+ require 'fixtures/article'
3
+ require 'fixtures/membership'
4
+ require 'fixtures/product'
5
+ require 'fixtures/tariff'
6
+ require 'fixtures/product_tariff'
7
+ require 'fixtures/suburb'
8
+ require 'fixtures/street'
9
+ require 'fixtures/restaurant'
10
+ require 'fixtures/dorm'
11
+ require 'fixtures/room'
12
+ require 'fixtures/room_attribute'
13
+ require 'fixtures/room_attribute_assignment'
14
+ require 'fixtures/student'
15
+ require 'fixtures/room_assignment'
16
+ require 'fixtures/user'
17
+ require 'fixtures/reading'
18
+
19
+ class TestAssociations < ActiveSupport::TestCase
20
+ fixtures :articles, :products, :tariffs, :product_tariffs, :suburbs, :streets, :restaurants, :restaurants_suburbs,
21
+ :dorms, :rooms, :room_attributes, :room_attribute_assignments, :students, :room_assignments, :users, :readings,
22
+ :memberships
23
+
24
+ def test_has_many_through_with_conditions_when_through_association_is_not_composite
25
+ user = User.find(:first)
26
+ assert_equal 1, user.articles.find(:all, :conditions => ["articles.name = ?", "Article One"]).size
27
+ end
28
+
29
+ def test_has_many_through_with_conditions_when_through_association_is_composite
30
+ room = Room.find(:first)
31
+ assert_equal 0, room.room_attributes.find(:all, :conditions => ["room_attributes.name != ?", "keg"]).size
32
+ end
33
+
34
+ def test_has_many_through_on_custom_finder_when_through_association_is_composite_finder_when_through_association_is_not_composite
35
+ user = User.find(:first)
36
+ assert_equal 1, user.find_custom_articles.size
37
+ end
38
+
39
+ def test_has_many_through_on_custom_finder_when_through_association_is_composite
40
+ room = Room.find(:first)
41
+ assert_equal 0, room.find_custom_room_attributes.size
42
+ end
43
+
44
+ def test_count
45
+ assert_equal 2, Product.count(:include => :product_tariffs)
46
+ assert_equal 3, Tariff.count(:include => :product_tariffs)
47
+ assert_equal 2, Tariff.count(:group => :start_date).size
48
+ end
49
+
50
+ def test_products
51
+ assert_not_nil products(:first_product).product_tariffs
52
+ assert_equal 2, products(:first_product).product_tariffs.length
53
+ assert_not_nil products(:first_product).tariffs
54
+ assert_equal 2, products(:first_product).tariffs.length
55
+ assert_not_nil products(:first_product).product_tariff
56
+ end
57
+
58
+ def test_product_tariffs
59
+ assert_not_nil product_tariffs(:first_flat).product
60
+ assert_not_nil product_tariffs(:first_flat).tariff
61
+ assert_equal Product, product_tariffs(:first_flat).product.class
62
+ assert_equal Tariff, product_tariffs(:first_flat).tariff.class
63
+ end
64
+
65
+ def test_tariffs
66
+ assert_not_nil tariffs(:flat).product_tariffs
67
+ assert_equal 1, tariffs(:flat).product_tariffs.length
68
+ assert_not_nil tariffs(:flat).products
69
+ assert_equal 1, tariffs(:flat).products.length
70
+ assert_not_nil tariffs(:flat).product_tariff
71
+ end
72
+
73
+ # Its not generating the instances of associated classes from the rows
74
+ def test_find_includes_products
75
+ assert @products = Product.find(:all, :include => :product_tariffs)
76
+ assert_equal 2, @products.length
77
+ assert_not_nil @products.first.instance_variable_get('@product_tariffs'), '@product_tariffs not set; should be array'
78
+ assert_equal 3, @products.inject(0) {|sum, tariff| sum + tariff.instance_variable_get('@product_tariffs').length},
79
+ "Incorrect number of product_tariffs returned"
80
+ end
81
+
82
+ def test_find_includes_tariffs
83
+ assert @tariffs = Tariff.find(:all, :include => :product_tariffs)
84
+ assert_equal 3, @tariffs.length
85
+ assert_not_nil @tariffs.first.instance_variable_get('@product_tariffs'), '@product_tariffs not set; should be array'
86
+ assert_equal 3, @tariffs.inject(0) {|sum, tariff| sum + tariff.instance_variable_get('@product_tariffs').length},
87
+ "Incorrect number of product_tariffs returnedturned"
88
+ end
89
+
90
+ def test_find_includes_product
91
+ assert @product_tariffs = ProductTariff.find(:all, :include => :product)
92
+ assert_equal 3, @product_tariffs.length
93
+ assert_not_nil @product_tariffs.first.instance_variable_get('@product'), '@product not set'
94
+ end
95
+
96
+ def test_find_includes_comp_belongs_to_tariff
97
+ assert @product_tariffs = ProductTariff.find(:all, :include => :tariff)
98
+ assert_equal 3, @product_tariffs.length
99
+ assert_not_nil @product_tariffs.first.instance_variable_get('@tariff'), '@tariff not set'
100
+ end
101
+
102
+ def test_find_includes_extended
103
+ assert @products = Product.find(:all, :include => {:product_tariffs => :tariff})
104
+ assert_equal 3, @products.inject(0) {|sum, product| sum + product.instance_variable_get('@product_tariffs').length},
105
+ "Incorrect number of product_tariffs returned"
106
+
107
+ assert @tariffs = Tariff.find(:all, :include => {:product_tariffs => :product})
108
+ assert_equal 3, @tariffs.inject(0) {|sum, tariff| sum + tariff.instance_variable_get('@product_tariffs').length},
109
+ "Incorrect number of product_tariffs returned"
110
+ end
111
+
112
+ def test_has_many_through
113
+ @products = Product.find(:all, :include => :tariffs)
114
+ assert_equal 3, @products.inject(0) {|sum, product| sum + product.instance_variable_get('@tariffs').length},
115
+ "Incorrect number of tariffs returned"
116
+ end
117
+
118
+ def test_has_many_through_when_not_pre_loaded
119
+ student = Student.find(:first)
120
+ rooms = student.rooms
121
+ assert_equal 1, rooms.size
122
+ assert_equal 1, rooms.first.dorm_id
123
+ assert_equal 1, rooms.first.room_id
124
+ end
125
+
126
+ def test_has_many_through_when_through_association_is_composite
127
+ dorm = Dorm.find(:first)
128
+ assert_equal 1, dorm.rooms.length
129
+ assert_equal 1, dorm.rooms.first.room_attributes.length
130
+ assert_equal 'keg', dorm.rooms.first.room_attributes.first.name
131
+ end
132
+
133
+ def test_associations_with_conditions
134
+ @suburb = Suburb.find([2, 1])
135
+ assert_equal 2, @suburb.streets.size
136
+
137
+ @suburb = Suburb.find([2, 1])
138
+ assert_equal 1, @suburb.first_streets.size
139
+
140
+ @suburb = Suburb.find([2, 1], :include => :streets)
141
+ assert_equal 2, @suburb.streets.size
142
+
143
+ @suburb = Suburb.find([2, 1], :include => :first_streets)
144
+ assert_equal 1, @suburb.first_streets.size
145
+ end
146
+
147
+ def test_has_and_belongs_to_many
148
+ @restaurant = Restaurant.find([1,1])
149
+ assert_equal 2, @restaurant.suburbs.size
150
+
151
+ @restaurant = Restaurant.find([1,1], :include => :suburbs)
152
+ assert_equal 2, @restaurant.suburbs.size
153
+ end
154
+
155
+ def test_has_many_with_primary_key
156
+ @membership = Membership.find([1, 1])
157
+
158
+ assert_equal 2, @membership.readings.size
159
+ end
160
+
161
+ def test_has_one_with_primary_key
162
+ @membership = Membership.find([1, 1])
163
+
164
+ assert_equal 2, @membership.reading.id
165
+ end
166
+
167
+ def test_joins_has_many_with_primary_key
168
+ @membership = Membership.find(:first, :joins => :readings, :conditions => { :readings => { :id => 1 } })
169
+
170
+ assert_equal [1, 1], @membership.id
171
+ end
172
+
173
+ def test_joins_has_one_with_primary_key
174
+ @membership = Membership.find(:first, :joins => :reading, :conditions => { :readings => { :id => 2 } })
175
+
176
+ assert_equal [1, 1], @membership.id
177
+ end
178
+ end
@@ -0,0 +1,22 @@
1
+ require 'abstract_unit'
2
+ require 'fixtures/kitchen_sink'
3
+ require 'fixtures/reference_type'
4
+
5
+ class TestAttributeMethods < ActiveSupport::TestCase
6
+ fixtures :kitchen_sinks, :reference_types
7
+
8
+ def test_read_attribute_with_single_key
9
+ rt = ReferenceType.find(1)
10
+ assert_equal(1, rt.reference_type_id)
11
+ assert_equal('NAME_PREFIX', rt.type_label)
12
+ assert_equal('Name Prefix', rt.abbreviation)
13
+ end
14
+
15
+ def test_read_attribute_with_composite_keys
16
+ sink = KitchenSink.find(1,2)
17
+ assert_equal(1, sink.id_1)
18
+ assert_equal(2, sink.id_2)
19
+ assert_equal(Date.today, sink.a_date.to_date)
20
+ assert_equal('string', sink.a_string)
21
+ end
22
+ end
@@ -0,0 +1,80 @@
1
+ require 'abstract_unit'
2
+ require 'fixtures/reference_type'
3
+ require 'fixtures/reference_code'
4
+ require 'fixtures/product'
5
+ require 'fixtures/tariff'
6
+ require 'fixtures/product_tariff'
7
+
8
+ class TestAttributes < ActiveSupport::TestCase
9
+ fixtures :reference_types, :reference_codes, :products, :tariffs, :product_tariffs
10
+
11
+ CLASSES = {
12
+ :single => {
13
+ :class => ReferenceType,
14
+ :primary_keys => :reference_type_id,
15
+ },
16
+ :dual => {
17
+ :class => ReferenceCode,
18
+ :primary_keys => [:reference_type_id, :reference_code],
19
+ },
20
+ }
21
+
22
+ def setup
23
+ self.class.classes = CLASSES
24
+ end
25
+
26
+ def test_brackets
27
+ testing_with do
28
+ @first.attributes.each_pair do |attr_name, value|
29
+ assert_equal value, @first[attr_name]
30
+ end
31
+ end
32
+ end
33
+
34
+ def test_brackets_primary_key
35
+ testing_with do
36
+ assert_equal @first.id, @first[@primary_keys], "[] failing for #{@klass}"
37
+ assert_equal @first.id, @first[@first.class.primary_key]
38
+ end
39
+ end
40
+
41
+ def test_brackets_assignment
42
+ testing_with do
43
+ @first.attributes.each_pair do |attr_name, value|
44
+ @first[attr_name]= !value.nil? ? value * 2 : '1'
45
+ assert_equal !value.nil? ? value * 2 : '1', @first[attr_name]
46
+ end
47
+ end
48
+ end
49
+
50
+ def test_brackets_foreign_key_assignment
51
+ @flat = Tariff.find(1, Date.today.to_s(:db))
52
+ @second_free = ProductTariff.find(2,2,Date.today.to_s(:db))
53
+ @second_free_fk = [:tariff_id, :tariff_start_date]
54
+
55
+ @second_free[key = @second_free_fk] = @flat.id
56
+ compare_indexes('@flat', @flat.class.primary_key, '@second_free', @second_free_fk)
57
+ assert_equal @flat.id, @second_free[key]
58
+
59
+ @second_free[key = @second_free_fk.to_composite_keys] = @flat.id
60
+ assert_equal @flat.id, @second_free[key]
61
+ compare_indexes('@flat', @flat.class.primary_key, '@second_free', @second_free_fk)
62
+
63
+ @second_free[key = @second_free_fk.to_composite_keys.to_s] = @flat.id
64
+ assert_equal @flat.id, @second_free[key]
65
+ compare_indexes('@flat', @flat.class.primary_key, '@second_free', @second_free_fk)
66
+ end
67
+
68
+ private
69
+
70
+ def compare_indexes(obj_name1, indexes1, obj_name2, indexes2)
71
+ obj1, obj2 = eval "[#{obj_name1}, #{obj_name2}]"
72
+ indexes1.length.times do |key_index|
73
+ assert_equal obj1[indexes1[key_index].to_s],
74
+ obj2[indexes2[key_index].to_s],
75
+ "#{obj_name1}[#{indexes1[key_index]}]=#{obj1[indexes1[key_index].to_s].inspect} != " +
76
+ "#{obj_name2}[#{indexes2[key_index]}]=#{obj2[indexes2[key_index].to_s].inspect}; " +
77
+ "#{obj_name2} = #{obj2.inspect}"
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,34 @@
1
+ require 'abstract_unit'
2
+ require 'fixtures/reference_type'
3
+ require 'fixtures/reference_code'
4
+
5
+ class TestClone < ActiveSupport::TestCase
6
+ fixtures :reference_types, :reference_codes
7
+
8
+ CLASSES = {
9
+ :single => {
10
+ :class => ReferenceType,
11
+ :primary_keys => :reference_type_id,
12
+ },
13
+ :dual => {
14
+ :class => ReferenceCode,
15
+ :primary_keys => [:reference_type_id, :reference_code],
16
+ },
17
+ }
18
+
19
+ def setup
20
+ self.class.classes = CLASSES
21
+ end
22
+
23
+ def test_truth
24
+ testing_with do
25
+ clone = @first.clone
26
+ assert_equal @first.attributes.block(@klass.primary_key), clone.attributes
27
+ if composite?
28
+ @klass.primary_key.each {|key| assert_nil clone[key], "Primary key '#{key}' should be nil"}
29
+ else
30
+ assert_nil clone[@klass.primary_key], "Sole primary key should be nil"
31
+ end
32
+ end
33
+ end
34
+ end