Class: SugarCRM::Module
- Inherits:
-
Object
- Object
- SugarCRM::Module
- Defined in:
- lib/sugarcrm/module.rb
Overview
A class for handling SugarCRM Modules
Instance Attribute Summary collapse
-
#custom_table_name ⇒ Object
Returns the value of attribute custom_table_name.
-
#fields ⇒ Object
Returns the fields associated with the module.
-
#klass ⇒ Object
(also: #bean)
Returns the value of attribute klass.
-
#link_fields ⇒ Object
Returns the value of attribute link_fields.
-
#name ⇒ Object
Returns the value of attribute name.
-
#table_name ⇒ Object
Returns the value of attribute table_name.
Class Method Summary collapse
-
.deregister_all(session) ⇒ Object
Deregisters all of the SugarCRM Modules.
-
.find(name, session = nil) ⇒ Object
Finds a module by name, or klass name.
-
.initialized? ⇒ Boolean
Class variable to track if we’ve initialized or not.
-
.register_all(session) ⇒ Object
Registers all of the SugarCRM Modules.
Instance Method Summary collapse
-
#custom_module? ⇒ Boolean
Return true if this module was created in the SugarCRM Studio (i.e. it is not part of the modules that ship in the default SugarCRM configuration).
-
#deregister ⇒ Object
Deregisters the module.
- #fields_registered? ⇒ Boolean (also: #link_fields_registered?)
-
#handle_empty_arrays ⇒ Object
TODO: Refactor this to be less repetitive.
-
#initialize(session, name) ⇒ Module
constructor
Dynamically register objects based on Module name I.e.
-
#register ⇒ Object
Registers a single module by name Adds module to SugarCRM.modules (SugarCRM.modules << Module.new(“Users”)) Adds module class to SugarCRM parent module (SugarCRM.constants << User) Note, SugarCRM::User.module == Module.find(“Users”).
- #registered? ⇒ Boolean
-
#required_fields ⇒ Object
Returns the required fields.
-
#resolve_custom_table_name ⇒ Object
Set table name for custom attibutes Custom attributes are contained in a table named after the module, with a ‘_cstm’ suffix.
- #to_class ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(session, name) ⇒ Module
Dynamically register objects based on Module name I.e. a SugarCRM Module named Users will generate a SugarCRM::User class.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/sugarcrm/module.rb', line 15 def initialize(session, name) @session = session # the session from which this module was retrieved @name = name @klass = name.classify unless custom_module? @table_name = name.tableize else @table_name = @name end @custom_table_name = resolve_custom_table_name @fields = {} @link_fields = {} @fields_registered = false self end |
Instance Attribute Details
#custom_table_name ⇒ Object
Returns the value of attribute custom_table_name.
6 7 8 |
# File 'lib/sugarcrm/module.rb', line 6 def custom_table_name @custom_table_name end |
#fields ⇒ Object
Returns the fields associated with the module
52 53 54 |
# File 'lib/sugarcrm/module.rb', line 52 def fields @fields end |
#klass ⇒ Object Also known as: bean
Returns the value of attribute klass.
7 8 9 |
# File 'lib/sugarcrm/module.rb', line 7 def klass @klass end |
#link_fields ⇒ Object
Returns the value of attribute link_fields.
9 10 11 |
# File 'lib/sugarcrm/module.rb', line 9 def link_fields @link_fields end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/sugarcrm/module.rb', line 4 def name @name end |
#table_name ⇒ Object
Returns the value of attribute table_name.
5 6 7 |
# File 'lib/sugarcrm/module.rb', line 5 def table_name @table_name end |
Class Method Details
.deregister_all(session) ⇒ Object
Deregisters all of the SugarCRM Modules
146 147 148 149 150 151 152 153 154 |
# File 'lib/sugarcrm/module.rb', line 146 def deregister_all(session) namespace = session.namespace_const session.modules.each do |m| m.deregister end session.modules = [] @initialized = false true end |
.find(name, session = nil) ⇒ Object
Finds a module by name, or klass name
157 158 159 160 161 162 163 164 165 |
# File 'lib/sugarcrm/module.rb', line 157 def find(name, session=nil) session ||= SugarCRM.session register_all(session) unless initialized? session.modules.each do |m| return m if m.name == name return m if m.klass == name end false end |
.initialized? ⇒ Boolean
Class variable to track if we’ve initialized or not
168 169 170 |
# File 'lib/sugarcrm/module.rb', line 168 def initialized? @initialized ||= false end |
.register_all(session) ⇒ Object
Registers all of the SugarCRM Modules
136 137 138 139 140 141 142 143 |
# File 'lib/sugarcrm/module.rb', line 136 def register_all(session) namespace = session.namespace_const session.connection.get_modules.each do |m| session.modules << m.register end @initialized = true true end |
Instance Method Details
#custom_module? ⇒ Boolean
Return true if this module was created in the SugarCRM Studio (i.e. it is not part of the modules that ship in the default SugarCRM configuration)
33 34 35 36 |
# File 'lib/sugarcrm/module.rb', line 33 def custom_module? # custom module names are all lower_case, whereas SugarCRM modules are CamelCase @name.downcase == @name end |
#deregister ⇒ Object
Deregisters the module
113 114 115 116 117 118 |
# File 'lib/sugarcrm/module.rb', line 113 def deregister return true unless registered? klass = self.klass @session.namespace_const.instance_eval{ remove_const klass } true end |
#fields_registered? ⇒ Boolean Also known as: link_fields_registered?
62 63 64 |
# File 'lib/sugarcrm/module.rb', line 62 def fields_registered? @fields_registered end |
#handle_empty_arrays ⇒ Object
TODO: Refactor this to be less repetitive
86 87 88 89 |
# File 'lib/sugarcrm/module.rb', line 86 def handle_empty_arrays @fields = {}.with_indifferent_access if @fields.length == 0 @link_fields = {}.with_indifferent_access if @link_fields.length == 0 end |
#register ⇒ Object
Registers a single module by name Adds module to SugarCRM.modules (SugarCRM.modules << Module.new(“Users”)) Adds module class to SugarCRM parent module (SugarCRM.constants << User) Note, SugarCRM::User.module == Module.find(“Users”)
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/sugarcrm/module.rb', line 95 def register return self if registered? mod_instance = self sess = @session # class Class < SugarCRM::Base # module_name = "Accounts" # end klass = Class.new(SugarCRM::Base) do self._module = mod_instance self.session = sess end # class Account < SugarCRM::Base @session.namespace_const.const_set self.klass, klass self end |
#registered? ⇒ Boolean
120 121 122 |
# File 'lib/sugarcrm/module.rb', line 120 def registered? @session.namespace_const.const_defined? @klass end |
#required_fields ⇒ Object
Returns the required fields
69 70 71 72 73 74 75 76 77 |
# File 'lib/sugarcrm/module.rb', line 69 def required_fields required_fields = [] ignore_fields = [:id, :date_entered, :date_modified] self.fields.each_value do |field| next if ignore_fields.include? field["name"].to_sym required_fields << field["name"].to_sym if field["required"] == 1 end required_fields end |
#resolve_custom_table_name ⇒ Object
Set table name for custom attibutes Custom attributes are contained in a table named after the module, with a ‘_cstm’ suffix. The module’s table name must be tableized for the modules that ship with SugarCRM. For custom modules (created in the Studio), table name don’t need to be tableized since the name passed to the constructor is already tableized
43 44 45 46 47 48 49 |
# File 'lib/sugarcrm/module.rb', line 43 def resolve_custom_table_name if custom_module? @custom_table_name = @name + "_cstm" else @custom_table_name = @table_name + "_cstm" end end |
#to_class ⇒ Object
128 129 130 |
# File 'lib/sugarcrm/module.rb', line 128 def to_class SugarCRM.const_get(@klass).new end |
#to_s ⇒ Object
124 125 126 |
# File 'lib/sugarcrm/module.rb', line 124 def to_s @klass end |