Skip to content

Commit

Permalink
Refactor and add spec
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcreekroad committed Nov 18, 2019
1 parent 3d28d17 commit 47ee071
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module ActiveRecord
class Base

def self.reflect_on_all_associations
@associations ||= superclass.instance_eval { (@associations && @associations.dup) || [] }
@associations ||= superclass.instance_eval { @associations&.dup || [] }
end

def self.reflect_on_association(attr)
Expand Down
17 changes: 17 additions & 0 deletions ruby/hyper-model/spec/batch7/aaa-unit_tests/associations_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'spec_helper'

describe 'Association Reflection', js: true do
it 'only knows associations defined in itself or its parent' do
expect_evaluate_ruby do
Dog.reflect_on_all_associations.map(&:attribute)
end.to eq(%w[owner bones])

expect_evaluate_ruby do
Cat.reflect_on_all_associations.map(&:attribute)
end.to eq(%w[owner scratching_posts])

expect_evaluate_ruby do
Pet.reflect_on_all_associations.map(&:attribute)
end.to eq(%w[owner])
end
end
3 changes: 3 additions & 0 deletions ruby/hyper-model/spec/test_app/app/models/public/bone.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Bone < ApplicationRecord
belongs_to :dog
end
5 changes: 5 additions & 0 deletions ruby/hyper-model/spec/test_app/app/models/public/cat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require_relative 'pet'

class Cat < Pet
has_many :scratching_posts
end
5 changes: 5 additions & 0 deletions ruby/hyper-model/spec/test_app/app/models/public/dog.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require_relative 'pet'

class Dog < Pet
has_many :bones
end
3 changes: 3 additions & 0 deletions ruby/hyper-model/spec/test_app/app/models/public/pet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Pet < ApplicationRecord
belongs_to :owner, class_name: 'User'
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ScratchingPost < ApplicationRecord
belongs_to :cat
end
4 changes: 4 additions & 0 deletions ruby/hyper-model/spec/test_app/app/models/public/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,7 @@ def name=(val) # this is here to test ability to save changes to this type of p
end

end unless RUBY_ENGINE == 'opal'

class User < ActiveRecord::Base
has_many :pets, inverse_of: :owner
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class CreateTestModels < ActiveRecord::Migration
class CreateTestModels < ActiveRecord::Migration[5.2]
def change
create_table :test_models do |t|
t.string :test_attribute
Expand Down Expand Up @@ -71,5 +71,17 @@ def change
t.integer "user_id"
t.integer "comment_id"
end

create_table :pets do |t|
t.integer :owner_id
end

create_table :bones do |t|
t.integer :dog_id
end

create_table :scratching_posts do |t|
t.integer :cat_id
end
end
end
132 changes: 75 additions & 57 deletions ruby/hyper-model/spec/test_app/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,92 +10,110 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160731182106) do
ActiveRecord::Schema.define(version: 2016_07_31_182106) do

create_table "addresses", force: :cascade do |t|
t.string "street"
t.string "city"
t.string "state"
t.string "zip"
create_table "addresses", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.string "street"
t.string "city"
t.string "state"
t.string "zip"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "child_models", force: :cascade do |t|
t.string "child_attribute"
t.integer "test_model_id"
create_table "bones", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.integer "dog_id"
end

create_table "comments", force: :cascade do |t|
t.text "comment"
create_table "child_models", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.string "child_attribute"
t.bigint "test_model_id"
t.index ["test_model_id"], name: "index_child_models_on_test_model_id"
end

create_table "comments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.text "comment"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "todo_id"
t.integer "author_id"
t.integer "user_id"
t.integer "todo_item_id"
t.bigint "todo_id"
t.bigint "author_id"
t.integer "user_id"
t.integer "todo_item_id"
t.index ["author_id"], name: "index_comments_on_author_id"
t.index ["todo_id"], name: "index_comments_on_todo_id"
end

create_table "hyperstack_connections", force: :cascade do |t|
t.string "channel"
t.string "session"
create_table "hyperstack_connections", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.string "channel"
t.string "session"
t.datetime "created_at"
t.datetime "expires_at"
t.datetime "refresh_at"
end

create_table "hyperstack_queued_messages", force: :cascade do |t|
t.text "data"
create_table "hyperstack_queued_messages", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.text "data"
t.integer "connection_id"
end

create_table "test_models", force: :cascade do |t|
t.string "test_attribute"
t.boolean "completed"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
create_table "pets", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.integer "owner_id"
end

create_table "scratching_posts", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.integer "cat_id"
end

create_table "test_models", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.string "test_attribute"
t.boolean "completed"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "todo_items", force: :cascade do |t|
t.string "title"
t.text "description"
t.boolean "complete"
create_table "todo_items", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.string "title"
t.text "description"
t.boolean "complete"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
t.integer "comment_id"
t.integer "user_id"
t.integer "comment_id"
end

create_table "todos", force: :cascade do |t|
t.string "title"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "completed", default: false, null: false
t.integer "created_by_id"
t.integer "owner_id"
create_table "todos", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.string "title"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "completed", default: false, null: false
t.bigint "created_by_id"
t.bigint "owner_id"
t.index ["created_by_id"], name: "index_todos_on_created_by_id"
t.index ["owner_id"], name: "index_todos_on_owner_id"
end

create_table "users", force: :cascade do |t|
t.string "role"
t.integer "manager_id"
t.string "first_name"
t.string "last_name"
t.string "email"
create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.string "role"
t.bigint "manager_id"
t.string "first_name"
t.string "last_name"
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
t.string "address_street"
t.string "address_city"
t.string "address_state"
t.string "address_zip"
t.integer "address_id"
t.string "address2_street"
t.string "address2_city"
t.string "address2_state"
t.string "address2_zip"
t.string "data_string"
t.integer "data_times"
t.integer "test_enum"
t.string "address_street"
t.string "address_city"
t.string "address_state"
t.string "address_zip"
t.integer "address_id"
t.string "address2_street"
t.string "address2_city"
t.string "address2_state"
t.string "address2_zip"
t.string "data_string"
t.integer "data_times"
t.integer "test_enum"
t.index ["manager_id"], name: "index_users_on_manager_id"
end

end

0 comments on commit 47ee071

Please sign in to comment.