Rails initializers only running in certain context (eg. server and not rake, generator, etc.) -


i'm in process of writing rails engine requires setup config , check see if required attributes defined on model. engine class in lib defines follows.

module kiosk    mattr_accessor :kiosk_class   @@kiosk_class = 'computer'    mattr_accessor :kiosk_primary_key   @@kiosk_primary_key = 'id'    mattr_accessor :kiosk_type_foreign_key   @@kiosk_type_foreign_key = 'kiosk_type_id'    mattr_accessor :kiosk_name_attribute   @@kiosk_name_attribute = 'name'    mattr_accessor :kiosk_ip_address_attribute   @@kiosk_ip_address_attribute = 'ip_address'    mattr_accessor :kiosk_mac_address_attribute   @@kiosk_mac_address_attribute = 'mac_address'    # private config , methods    def self.setup     if block_given?       yield self     end      check_fields!   end    def self.required_fields     [@@kiosk_primary_key, @@kiosk_name_attribute, @@kiosk_ip_address_attribute, @@kiosk_mac_address_attribute, @@kiosk_type_foreign_key]   end    def self.check_fields!     failed_attributes = []     instance = @@kiosk_class.constantize.new      required_fields.each |field|       failed_attributes << field unless instance.respond_to?(field)     end      if failed_attributes.any?       raise "missing required attributes in #{@@kiosk_class} model: #{failed_attributes.join(', ')}"     end   end    def self.klass     @@kiosk_class.constantize   end  end 

the model name , attribute names can configured in required initializer job of calling setup passing block of config parameters. is,

kiosk.setup |config|    # name of class stores info kiosks   # should contain required fields names defined below   # config.kiosk_class = 'computer'    # primary key of kiosk class   # config.kiosk_primary_key = 'id'    # foreign key in kiosk class kiosk type   # config.kiosk_type_foreign_key = 'kiosk_type_id'    # attribute containing name of kiosk   # config.kiosk_name_attribute = 'name'    # attribute containing ip address of kiosk   # config.kiosk_ip_address_attribute = 'ip_address'    # attribute containing mac address of kiosk   # config.kiosk_mac_address_attribute = 'mac_address'  end 

the problem encountered during testing if required attribute missing calling generator or rake task fails meaning attribute cannot added.

what able detect in setup whether being called part of server start (and therefore should fields check) or other rails startup such rake tasks, generators, etc. (and therefore should skip fields check). feel there must solution because starting rails console never fails.

alternatively, if not possible, how perform fields check outside of initializer in way guaranteed occur during server startup , have happen once per startup?

i realise similar questions have been asked before on knowing context app running under (e.g. how prevent initializers running when running `rails generate` , rails 3 initializers run on `rails server` , not `rails generate`, etc) solutions presented there not useful situation.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -