Skip to content

Commit 47ee071

Browse files
committed
Refactor and add spec
1 parent 3d28d17 commit 47ee071

10 files changed

Lines changed: 129 additions & 59 deletions

File tree

ruby/hyper-model/lib/reactive_record/active_record/associations.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module ActiveRecord
33
class Base
44

55
def self.reflect_on_all_associations
6-
@associations ||= superclass.instance_eval { (@associations && @associations.dup) || [] }
6+
@associations ||= superclass.instance_eval { @associations&.dup || [] }
77
end
88

99
def self.reflect_on_association(attr)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'spec_helper'
2+
3+
describe 'Association Reflection', js: true do
4+
it 'only knows associations defined in itself or its parent' do
5+
expect_evaluate_ruby do
6+
Dog.reflect_on_all_associations.map(&:attribute)
7+
end.to eq(%w[owner bones])
8+
9+
expect_evaluate_ruby do
10+
Cat.reflect_on_all_associations.map(&:attribute)
11+
end.to eq(%w[owner scratching_posts])
12+
13+
expect_evaluate_ruby do
14+
Pet.reflect_on_all_associations.map(&:attribute)
15+
end.to eq(%w[owner])
16+
end
17+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Bone < ApplicationRecord
2+
belongs_to :dog
3+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require_relative 'pet'
2+
3+
class Cat < Pet
4+
has_many :scratching_posts
5+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require_relative 'pet'
2+
3+
class Dog < Pet
4+
has_many :bones
5+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Pet < ApplicationRecord
2+
belongs_to :owner, class_name: 'User'
3+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class ScratchingPost < ApplicationRecord
2+
belongs_to :cat
3+
end

ruby/hyper-model/spec/test_app/app/models/public/user.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,7 @@ def name=(val) # this is here to test ability to save changes to this type of p
8686
end
8787

8888
end unless RUBY_ENGINE == 'opal'
89+
90+
class User < ActiveRecord::Base
91+
has_many :pets, inverse_of: :owner
92+
end

ruby/hyper-model/spec/test_app/db/migrate/20160731182106_create_test_models.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class CreateTestModels < ActiveRecord::Migration
1+
class CreateTestModels < ActiveRecord::Migration[5.2]
22
def change
33
create_table :test_models do |t|
44
t.string :test_attribute
@@ -71,5 +71,17 @@ def change
7171
t.integer "user_id"
7272
t.integer "comment_id"
7373
end
74+
75+
create_table :pets do |t|
76+
t.integer :owner_id
77+
end
78+
79+
create_table :bones do |t|
80+
t.integer :dog_id
81+
end
82+
83+
create_table :scratching_posts do |t|
84+
t.integer :cat_id
85+
end
7486
end
7587
end

ruby/hyper-model/spec/test_app/db/schema.rb

Lines changed: 75 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -10,92 +10,110 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 20160731182106) do
13+
ActiveRecord::Schema.define(version: 2016_07_31_182106) do
1414

15-
create_table "addresses", force: :cascade do |t|
16-
t.string "street"
17-
t.string "city"
18-
t.string "state"
19-
t.string "zip"
15+
create_table "addresses", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
16+
t.string "street"
17+
t.string "city"
18+
t.string "state"
19+
t.string "zip"
2020
t.datetime "created_at"
2121
t.datetime "updated_at"
2222
end
2323

24-
create_table "child_models", force: :cascade do |t|
25-
t.string "child_attribute"
26-
t.integer "test_model_id"
24+
create_table "bones", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
25+
t.integer "dog_id"
2726
end
2827

29-
create_table "comments", force: :cascade do |t|
30-
t.text "comment"
28+
create_table "child_models", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
29+
t.string "child_attribute"
30+
t.bigint "test_model_id"
31+
t.index ["test_model_id"], name: "index_child_models_on_test_model_id"
32+
end
33+
34+
create_table "comments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
35+
t.text "comment"
3136
t.datetime "created_at"
3237
t.datetime "updated_at"
33-
t.integer "todo_id"
34-
t.integer "author_id"
35-
t.integer "user_id"
36-
t.integer "todo_item_id"
38+
t.bigint "todo_id"
39+
t.bigint "author_id"
40+
t.integer "user_id"
41+
t.integer "todo_item_id"
42+
t.index ["author_id"], name: "index_comments_on_author_id"
43+
t.index ["todo_id"], name: "index_comments_on_todo_id"
3744
end
3845

39-
create_table "hyperstack_connections", force: :cascade do |t|
40-
t.string "channel"
41-
t.string "session"
46+
create_table "hyperstack_connections", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
47+
t.string "channel"
48+
t.string "session"
4249
t.datetime "created_at"
4350
t.datetime "expires_at"
4451
t.datetime "refresh_at"
4552
end
4653

47-
create_table "hyperstack_queued_messages", force: :cascade do |t|
48-
t.text "data"
54+
create_table "hyperstack_queued_messages", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
55+
t.text "data"
4956
t.integer "connection_id"
5057
end
5158

52-
create_table "test_models", force: :cascade do |t|
53-
t.string "test_attribute"
54-
t.boolean "completed"
55-
t.datetime "created_at", null: false
56-
t.datetime "updated_at", null: false
59+
create_table "pets", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
60+
t.integer "owner_id"
61+
end
62+
63+
create_table "scratching_posts", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
64+
t.integer "cat_id"
65+
end
66+
67+
create_table "test_models", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
68+
t.string "test_attribute"
69+
t.boolean "completed"
70+
t.datetime "created_at", null: false
71+
t.datetime "updated_at", null: false
5772
end
5873

59-
create_table "todo_items", force: :cascade do |t|
60-
t.string "title"
61-
t.text "description"
62-
t.boolean "complete"
74+
create_table "todo_items", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
75+
t.string "title"
76+
t.text "description"
77+
t.boolean "complete"
6378
t.datetime "created_at"
6479
t.datetime "updated_at"
65-
t.integer "user_id"
66-
t.integer "comment_id"
80+
t.integer "user_id"
81+
t.integer "comment_id"
6782
end
6883

69-
create_table "todos", force: :cascade do |t|
70-
t.string "title"
71-
t.text "description"
72-
t.datetime "created_at", null: false
73-
t.datetime "updated_at", null: false
74-
t.boolean "completed", default: false, null: false
75-
t.integer "created_by_id"
76-
t.integer "owner_id"
84+
create_table "todos", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
85+
t.string "title"
86+
t.text "description"
87+
t.datetime "created_at", null: false
88+
t.datetime "updated_at", null: false
89+
t.boolean "completed", default: false, null: false
90+
t.bigint "created_by_id"
91+
t.bigint "owner_id"
92+
t.index ["created_by_id"], name: "index_todos_on_created_by_id"
93+
t.index ["owner_id"], name: "index_todos_on_owner_id"
7794
end
7895

79-
create_table "users", force: :cascade do |t|
80-
t.string "role"
81-
t.integer "manager_id"
82-
t.string "first_name"
83-
t.string "last_name"
84-
t.string "email"
96+
create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
97+
t.string "role"
98+
t.bigint "manager_id"
99+
t.string "first_name"
100+
t.string "last_name"
101+
t.string "email"
85102
t.datetime "created_at"
86103
t.datetime "updated_at"
87-
t.string "address_street"
88-
t.string "address_city"
89-
t.string "address_state"
90-
t.string "address_zip"
91-
t.integer "address_id"
92-
t.string "address2_street"
93-
t.string "address2_city"
94-
t.string "address2_state"
95-
t.string "address2_zip"
96-
t.string "data_string"
97-
t.integer "data_times"
98-
t.integer "test_enum"
104+
t.string "address_street"
105+
t.string "address_city"
106+
t.string "address_state"
107+
t.string "address_zip"
108+
t.integer "address_id"
109+
t.string "address2_street"
110+
t.string "address2_city"
111+
t.string "address2_state"
112+
t.string "address2_zip"
113+
t.string "data_string"
114+
t.integer "data_times"
115+
t.integer "test_enum"
116+
t.index ["manager_id"], name: "index_users_on_manager_id"
99117
end
100118

101119
end

0 commit comments

Comments
 (0)