Skip to content

Commit 871362c

Browse files
committed
Merge pull request #328 from cerebris/init_from_model
Init from model
2 parents 6249695 + 84c8892 commit 871362c

9 files changed

Lines changed: 158 additions & 38 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ end
262262

263263
The relationship methods (`relationship`, `has_one`, and `has_many`) support the following options:
264264

265-
* `class_name` - a string specifying the underlying class for the related resource
265+
* `class_name` - a string specifying the underlying class for the related resource. Defaults to the `class_name` property on the underlying model.
266266
* `foreign_key` - the method on the resource used to fetch the related resource. Defaults to `<resource_name>_id` for has_one and `<resource_name>_ids` for has_many relationships.
267267
* `acts_as_set` - allows the entire set of related records to be replaced in one operation. Defaults to false if not set.
268268
* `polymorphic` - set to true to identify relationships that are polymorphic.

lib/jsonapi/resource.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,15 @@ def _add_relationship(klass, *attrs)
673673

674674
attrs.each do |attr|
675675
check_reserved_relationship_name(attr)
676+
677+
# Initialize from an ActiveRecord model's properties
678+
if _model_class < ActiveRecord::Base
679+
model_association = _model_class.reflect_on_association(attr)
680+
if model_association
681+
options[:class_name] ||= model_association.class_name
682+
end
683+
end
684+
676685
@_relationships[attr] = relationship = klass.new(attr, options)
677686

678687
associated_records_method_name = case relationship

test/fixtures/active_record.rb

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,21 @@ class Tag < ActiveRecord::Base
254254
class Section < ActiveRecord::Base
255255
end
256256

257+
class HairCut < ActiveRecord::Base
258+
end
259+
260+
class Property < ActiveRecord::Base
261+
end
262+
263+
class Customer < ActiveRecord::Base
264+
end
265+
266+
class BadlyNamedAttributes < ActiveRecord::Base
267+
end
268+
269+
class Cat < ActiveRecord::Base
270+
end
271+
257272
class IsoCurrency < ActiveRecord::Base
258273
self.primary_key = :code
259274
# has_many :expense_entries, foreign_key: 'currency_code'
@@ -422,13 +437,6 @@ class Product < ActiveRecord::Base
422437
has_one :picture, as: :imageable
423438
end
424439

425-
### PORO Data - don't do this in a production app
426-
$breed_data = BreedData.new
427-
$breed_data.add(Breed.new(0, 'persian'))
428-
$breed_data.add(Breed.new(1, 'siamese'))
429-
$breed_data.add(Breed.new(2, 'sphinx'))
430-
$breed_data.add(Breed.new(3, 'to_delete'))
431-
432440
### OperationsProcessor
433441
class CountingActiveRecordOperationsProcessor < ActiveRecordOperationsProcessor
434442
after_find_operation do
@@ -896,8 +904,8 @@ def self.verify_key(key, context = nil)
896904
class PreferencesResource < JSONAPI::Resource
897905
attribute :advanced_mode
898906

899-
has_one :author, foreign_key: :person_id, class_name: 'Person'
900-
has_many :friends, class_name: 'Person'
907+
has_one :author, foreign_key: :person_id
908+
has_many :friends
901909

902910
def self.find_by_key(key, options = {})
903911
new(Preferences.first)
@@ -961,7 +969,7 @@ class PostResource < JSONAPI::Resource
961969
attribute :body
962970
attribute :subject
963971

964-
has_one :writer, foreign_key: 'author_id'
972+
has_one :writer, foreign_key: 'author_id', class_name: 'Writer'
965973
has_one :section
966974
has_many :comments, acts_as_set: false
967975

@@ -1246,30 +1254,9 @@ class BadlyNamedAttributesResource < JSONAPI::Resource
12461254
end
12471255
warn 'end testing Name Collisions'
12481256

1249-
### PORO DATA
1250-
gas_giant = PlanetType.create(name: 'Gas Giant')
1251-
planetoid = PlanetType.create(name: 'Planetoid')
1252-
terrestrial = PlanetType.create(name: 'Terrestrial')
1253-
sulfuric = PlanetType.create(name: 'Sulfuric')
1254-
unknown = PlanetType.create(name: 'unknown')
1255-
1256-
saturn = Planet.create(name: 'Satern',
1257-
description: 'Saturn is the sixth planet from the Sun and the second largest planet in the Solar System, after Jupiter.',
1258-
planet_type_id: planetoid.id)
1259-
titan = Moon.create(name:'Titan', description: 'Best known of the Saturn moons.', planet_id: saturn.id)
1260-
crater1 = Crater.create(code:'S56D', description: 'Very large crater', moon_id: titan.id)
1261-
crater2 = Crater.create(code:'A4D3', description: 'Small crater', moon_id: titan.id)
1262-
makemake = Planet.create(name: 'Makemake', description: 'A small planetoid in the Kuiperbelt.', planet_type_id: planetoid.id)
1263-
uranus = Planet.create(name: 'Uranus', description: 'Insert adolescent jokes here.', planet_type_id: gas_giant.id)
1264-
jupiter = Planet.create(name: 'Jupiter', description: 'A gas giant.', planet_type_id: gas_giant.id)
1265-
betax = Planet.create(name: 'Beta X', description: 'Newly discovered Planet X', planet_type_id: unknown.id)
1266-
betay = Planet.create(name: 'Beta X', description: 'Newly discovered Planet Y', planet_type_id: unknown.id)
1267-
betaz = Planet.create(name: 'Beta X', description: 'Newly discovered Planet Z', planet_type_id: unknown.id)
1268-
betaw = Planet.create(name: 'Beta W', description: 'Newly discovered Planet W')
1269-
Category.create(name: 'Category A', status: 'active')
1270-
Category.create(name: 'Category B', status: 'active')
1271-
Category.create(name: 'Category C', status: 'active')
1272-
Category.create(name: 'Category D', status: 'inactive')
1273-
Category.create(name: 'Category E', status: 'inactive')
1274-
Category.create(name: 'Category F', status: 'inactive')
1275-
Category.create(name: 'Category G', status: 'inactive')
1257+
### PORO Data - don't do this in a production app
1258+
$breed_data = BreedData.new
1259+
$breed_data.add(Breed.new(0, 'persian'))
1260+
$breed_data.add(Breed.new(1, 'siamese'))
1261+
$breed_data.add(Breed.new(2, 'sphinx'))
1262+
$breed_data.add(Breed.new(3, 'to_delete'))

test/fixtures/categories.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
category_a:
2+
id: 1
3+
name: Category A
4+
status: active
5+
6+
category_b:
7+
id: 2
8+
name: Category B
9+
status: active
10+
11+
category_c:
12+
id: 3
13+
name: Category C
14+
status: active
15+
16+
category_d:
17+
id: 4
18+
name: Category D
19+
status: inactive
20+
21+
category_e:
22+
id: 5
23+
name: Category E
24+
status: inactive
25+
26+
category_f:
27+
id: 6
28+
name: Category F
29+
status: inactive
30+
31+
category_g:
32+
id: 7
33+
name: Category G
34+
status: inactive
35+

test/fixtures/craters.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
crater1:
2+
code: S56D
3+
description: Very large crater
4+
moon_id: 1
5+
6+
crater2:
7+
code: A4D3
8+
description: Small crater
9+
moon_id: 1

test/fixtures/moons.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
titan:
2+
id: 1
3+
name: Titan
4+
description: Best known of the Saturn moons.
5+
planet_id: 1
6+

test/fixtures/planet_types.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
gas_giant:
2+
id: 1
3+
name: Gas Giant
4+
5+
planetoid:
6+
id: 2
7+
name: Planetoid
8+
9+
terrestrial:
10+
id: 3
11+
name: Terrestrial
12+
13+
sulfuric:
14+
id: 4
15+
name: Sulfuric
16+
17+
unknown:
18+
id: 5
19+
name: unknown

test/fixtures/planets.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
saturn:
2+
id: 1
3+
name: Satern
4+
description: Saturn is the sixth planet from the Sun and the second largest planet in the Solar System, after Jupiter.
5+
planet_type_id: 2
6+
7+
makemake:
8+
id: 2
9+
name: Makemake
10+
description: A small planetoid in the Kuiperbelt.
11+
planet_type_id: 2
12+
13+
uranus:
14+
id: 3
15+
name: Uranus
16+
description: Insert adolescent jokes here.
17+
planet_type_id: 1
18+
19+
jupiter:
20+
id: 4
21+
name: Jupiter
22+
description: A gas giant.
23+
planet_type_id: 1
24+
25+
betax:
26+
id: 5
27+
name: Beta X
28+
description: Newly discovered Planet X
29+
planet_type_id: 5
30+
31+
betay:
32+
id: 6
33+
name: Beta X
34+
description: Newly discovered Planet Y
35+
planet_type_id: 5
36+
37+
betaz:
38+
id: 7
39+
name: Beta X
40+
description: Newly discovered Planet Z
41+
planet_type_id: 5
42+
43+
betaw:
44+
id: 8
45+
name: Beta W
46+
description: Newly discovered Planet W
47+
planet_type_id:

test/test_helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,14 @@ class Minitest::Test
226226
include Helpers::Assertions
227227
include Helpers::ValueMatchers
228228
include Helpers::FunctionalHelpers
229+
include ActiveRecord::TestFixtures
230+
231+
def run_in_transaction?
232+
true
233+
end
234+
235+
self.fixture_path = "#{Rails.root}/fixtures"
236+
fixtures :all
229237
end
230238

231239
class ActiveSupport::TestCase

0 commit comments

Comments
 (0)