orbit4-5/lib/orbit_app/module/registration.rb

196 lines
4.8 KiB
Ruby

#This is a core class of Orbit Kernel. This class will register a module and set the properties defined in the module's initializer
#file.
module OrbitApp
module Module
module Registration
module ClassMethods
#initiate a blank array for registration
cattr_accessor :registrations
@@registrations = []
def new(name,&block)
@@registrations << RegisteredModule.new(name,&block)
end
def find_by_key(key)
@@registrations.each{|t|
return t if t.key.eql?(key)
}
return nil
end
def find_by_url(url)
@@registrations.each{|t|
sb = t.get_side_bar
links = sb.get_active_for_controllers rescue []
return t if links.include?url
}
end
def all
return @@registrations
end
end
extend ClassMethods
def self.included(other)
other.extend( ClassMethods )
end
class RegisteredModule
attr_reader :name,:key,:base_path, :module_label,:widget_methods,:authorizable_models,:is_authorizable, :data_count, :widget_settings, :icon_class_no_sidebar,:desktop_enabled
def initialize(name,&block)
@name = name
@key = @name.underscore.singularize
@side_bar = nil
@module_label = @name
@widget_methods = []
@widget_settings = {}
@is_taggable = false
@authorizable_models = []
@is_authorizable = false
@desktop_enabled = false
@taggable_model = nil
@is_categorizable = false
@is_frontend_enabled = false
@data_count = nil
@icon_class_no_sidebar = nil
block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given?
setup_module_app
end
def get_module_app
ModuleApp.find_by(key: @key, title: name) rescue nil
end
def setup_module_app
module_app = get_module_app
module_app = ModuleApp.new(:key=>@key,:title=>name) if module_app.nil?
module_app.refetch_setting!(self)
module_app.save(:validate=>false)
end
%w{data_count module_label icon_class_no_sidebar category base_url version organization author intro update_info}.each do |field|
define_method(field){|var| instance_variable_set( "@" + field, var)}
end
def module_label(name = @name)
@module_label = name
end
def widget_methods(widgets)
@widget_methods = widgets
end
def get_widget_methods
@widget_methods
end
def widget_settings(settings)
@widget_settings = settings.first
end
def get_widget_settings
@widget_settings
end
def get_icon_class
@side_bar.get_icon_class
end
def side_bar(&block) #setter for side_bar from init
@side_bar = SideBarRegistration::SideBar.new(@name,@key,method(:get_module_app), &block)
end
def get_side_bar
@side_bar
end
def icon_class
(@side_bar.get_icon_class rescue nil || @get_icon_class_no_sidebar)
end
def taggable(model)
@is_taggable = true
@taggable_model = model.classify
end
def is_taggable
@is_taggable
end
def taggable_model
@taggable_model
end
def authorizable
@is_authorizable = true
authorizable_on
end
def desktop_enabled(status)
@desktop_enabled = status
end
def is_desktop_enabled
@desktop_enabled
end
def is_authorizable
@is_authorizable
end
def categorizable
@is_categorizable = true
end
def is_categorizable
@is_categorizable
end
def authorizable_on(klass = 'Category')
@authorizable_models << klass
end
def get_authorizable_models
@authorizable_models
end
def frontend_enabled
@is_frontend_enabled = true
end
def is_frontend_enabled
@is_frontend_enabled
end
def is_widget_enabled
!@widget_methods.blank?
end
def data_count(range)
@data_count = range
end
def get_data_count
@data_count
end
def icon_class_no_sidebar(icon_class)
@icon_class_no_sidebar = icon_class
end
def get_icon_class_no_sidebar
@icon_class_no_sidebar
end
def personal_plugin(params) #setter for personal_plugin from init
Plugin::Registration.new_from_module_app(@name,@key,@base_path,params)
end
end
end
end
end