metaprogramming models

43 views
Skip to first unread message

loveand light

unread,
Mar 24, 2016, 10:55:27 AM3/24/16
to Ruby on Rails: Core
Hi,

I am curious if anyone would know how to reload a meta-model into what seems to be the global constants in Rails. Basically, this is a factory that creates models on the fly that contain AASM workflows, validations and other goodies. I can get this to work correctly if I restart the server but I want to be able 'reset' the constant that references the meta model if the code needs a refresh.  Does anyone know a way to do this? This code below works for the current process but I need this to refresh much like what happens when one adds a new file and ActiveSupport::Dependencies pick it up and reloads it.



At present,

    def reset_udt_const
      cn = udt_class_name
      if Object.const_defined?(cn)
       Object.instance_eval{remove_const(:"#{cn}")}
      end
      to_udt_model.reset_column_information

      MetaMark::DynamicRouter.reload
    end

Any help would be very appreciated. 

Thanks in advance.
Mark

Xavier Noria

unread,
Mar 25, 2016, 4:37:37 AM3/25/16
to rubyonrails-core
This departs from a fundamental convention in constant autoloading:

Constant Foo is expected to be autoloaded from a file called foo.rb
that lives somewhere in config.autoload_paths.

In your situation there are no files for these models, so I would like
to understand the details further to see the implications and whether
there is a way to do what you want.

For example, let's suppose there is a metaprogrammed model is called
Foo. What happens if I do

bin/rails runner Foo

Does it work? If it does, how is that definition on-the-fly
implemented in your application?

loveand light

unread,
Mar 25, 2016, 12:06:40 PM3/25/16
to Ruby on Rails: Core
Hi Xavier.

Right you are about the files and autoloading. All of the files that I was meta~programming were just ActiveRecord models, so what I did was take what was being instance/class_eval'd and dumped it into a model class. 

So now when I make a change to the model, say change up a workflow (which is really a gui version of AASM with several modifications) or add a new has_many field and  need these changes to take effect they will because the model file will be re-written at the end of that process.. Ok, so that is good for right now, but I need to demo this on a production server asap. So really my problem is convincing the production server that it needs to re-compile  this model. I am reading up of a good way to do this but unfortunately I haven't found anything that will help me...yet. Phusion Passenger will allow you to re-start the app but doesn't have any way to tell it to recompile a file based on the last time it was modified.

I have my ear to the ground if anyone has any ideas. As of right now, I am not sure this is possible but...there is always a way ;)

Back in a bit!

Cheers,
Mark

Xavier Noria

unread,
Mar 25, 2016, 2:25:01 PM3/25/16
to rubyonrails-core
Wait, wait, let me understand better the boundaries of this problem.

Could you reply to my question above? If Foo is a metaprogrammed class, does

bin/rails runner Foo

work? If yes, how?

Once I know the answer to this, more questions will surely follow.
Reply all
Reply to author
Forward
0 new messages