|
| 1 | +require 'spec_helper' |
| 2 | +describe "self referencing belongs_to", js: true do |
| 3 | + |
| 4 | + before(:all) do |
| 5 | + require 'pusher' |
| 6 | + require 'pusher-fake' |
| 7 | + Pusher.app_id = "MY_TEST_ID" |
| 8 | + Pusher.key = "MY_TEST_KEY" |
| 9 | + Pusher.secret = "MY_TEST_SECRET" |
| 10 | + require "pusher-fake/support/base" |
| 11 | + |
| 12 | + Hyperstack.configuration do |config| |
| 13 | + config.transport = :pusher |
| 14 | + config.channel_prefix = "synchromesh" |
| 15 | + config.opts = {app_id: Pusher.app_id, key: Pusher.key, secret: Pusher.secret}.merge(PusherFake.configuration.web_options) |
| 16 | + end |
| 17 | + end |
| 18 | + |
| 19 | + before(:all) do |
| 20 | + class ActiveRecord::Base |
| 21 | + class << self |
| 22 | + def public_columns_hash |
| 23 | + @public_columns_hash ||= {} |
| 24 | + end |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + class SelfRefModel < ActiveRecord::Base |
| 29 | + def self.build_tables |
| 30 | + connection.create_table :self_ref_models, force: true do |t| |
| 31 | + t.string :name |
| 32 | + t.string :other |
| 33 | + t.belongs_to :parent |
| 34 | + t.timestamps |
| 35 | + end |
| 36 | + ActiveRecord::Base.public_columns_hash[name] = columns_hash |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + SelfRefModel.build_tables |
| 41 | + end |
| 42 | + |
| 43 | + before(:each) do |
| 44 | + |
| 45 | + stub_const 'TestApplication', Class.new |
| 46 | + stub_const 'TestApplicationPolicy', Class.new |
| 47 | + |
| 48 | + TestApplicationPolicy.class_eval do |
| 49 | + always_allow_connection |
| 50 | + regulate_all_broadcasts { |policy| policy.send_all } |
| 51 | + end |
| 52 | + |
| 53 | + isomorphic do |
| 54 | + class SelfRefModel < ActiveRecord::Base |
| 55 | + # the failure is caused by the has_many relationship coming first... |
| 56 | + has_many :children, class_name: 'SelfRefModel', foreign_key: 'parent_id' |
| 57 | + belongs_to :parent, class_name: 'SelfRefModel', required: false |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + SelfRefModel.create(name: 'first model') |
| 62 | + SelfRefModel.create(name: 'second model') |
| 63 | + |
| 64 | + mount 'SelfRefModelIndex' do |
| 65 | + class SelfRefModelIndex < HyperComponent |
| 66 | + render(UL) do |
| 67 | + SelfRefModel.first.tap do |m| |
| 68 | + if m.parent&.id |
| 69 | + LI { "name: #{m.name}, parent id: #{m.parent.id}." } |
| 70 | + elsif !m.children.empty? |
| 71 | + LI { "name: #{m.name}, children: [#{m.children.collect(&:id)}]"} |
| 72 | + else |
| 73 | + LI { "name: #{m.name}." } |
| 74 | + end |
| 75 | + end |
| 76 | + end |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + size_window(:small, :portrait) |
| 81 | + end |
| 82 | + |
| 83 | + it 'will be updated properly' do |
| 84 | + SelfRefModel.last.update(parent: SelfRefModel.first) |
| 85 | + expect(page).to have_content('name: first model, children: [2]') |
| 86 | + SelfRefModel.last.update(parent: nil) |
| 87 | + expect(page).to have_content('name: first model.') |
| 88 | + SelfRefModel.first.update(parent: SelfRefModel.last) |
| 89 | + expect(page).to have_content('name: first model, parent id: 2.') |
| 90 | + end |
| 91 | +end |
0 commit comments