ruby-hyperloop / hyper-mesh

The project has moved to Hyperstack!! - Synchronization of active record models across multiple clients using Pusher, ActionCable, or Polling
https://hyperstack.org/
MIT License
22 stars 12 forks source link

`window.ReactiveRecordPublicColumnsHash` includes complete DB schema #33

Closed janbiedermann closed 6 years ago

janbiedermann commented 7 years ago

my config: 2.4.1 :006 > Hyperloop.public_model_directories => ["app/models/public", "app/hyperloop/models"]

All my models descent from ApplicationRecord which extends ActiveRecord::Base. Because i have models in hyper loop/models and rails models i moved ApplicationRecord to hyperloop/models, this way its available for both hyperloop/models and rails models. i cite from the docs:

For Hyperloop to see this change, this file needs to be moved (or copied if you have some server-side models) to the apps/hyperloop folder.

But then hyper-mesh public_columns_hash.rb includes all my models in window.ReactiveRecordPublicColumnsHash because it walks all descendants from ActiveRecord::Base in app/hyperloop/models, which includes ApplicationRecord, and from it on all descentants of ApplicationRecord too.

public_columns_hash should only walk up models in public_models_directories

As a workaround one could put all functionality in a module, create ApplicationRecordPublic in app/hyperloop/models inherit public models from it and include FunctionalityModule, also in app/models ApplicationRecord for non public models.

janbiedermann commented 7 years ago

possible fix checking for each model if file exists in public dirs (variable called 'found'), following rails autoload naming conventions hyper-mesh/lib/reactive_record/active_record/public_columns_hash.rb

module Hyperloop
  define_setting :public_model_directories, ['app/models/public', 'app/hyperloop/models']
end

module ActiveRecord
  # adds method to get the HyperMesh public column types
  # for now we are just getting all models column types, but we should
  # look through the public folder, and just get those models.
  # this works because the public folder is currently required to be eaer loaded.
  class Base
    def self.public_columns_hash
      return @public_columns_hash if @public_columns_hash
      Hyperloop.public_model_directories.each do |dir|
        Dir.glob(Rails.root.join("#{dir}/*.rb")).each do |file|
          require_dependency(file)
        end
      end
      @public_columns_hash = {}
      descendants.each do |model|
        found = false
        Hyperloop.public_model_directories.each do |dir|
          found ||= File.exist?("#{dir}/#{model.name.underscore}.rb")
          break if found
        end
        next unless found
        @public_columns_hash[model.name] = model.columns_hash rescue nil
      end
      @public_columns_hash
    end
  end
end
janbiedermann commented 6 years ago

fixed in sachsenring

catmando commented 6 years ago

nice