Module: Models

Included in:
Ability, RolesController
Defined in:
lib/generators/mcms_authentication/templates/models.rb

Overview

@FileName: models.rb

@Company Name and Copyright information: Mindfire Solutions Pvt. Ltd.

@Creator name and date: Indranil Mukherjee 12/06/2012

@Description of the file contents: Defining all the routes

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_module_name(models) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/generators/mcms_authentication/templates/models.rb', line 125

def self.get_module_name(models)

  
  conactenated_string = ""

  @@tmplength = 0

  @@final_string = ""
  
  models.each do |model|
    
    conactenated_string = conactenated_string + model

    
    
  end

  models.each do |model|

    tmp = model.underscore.split("_")

    
    
    tmp.each do |t|

      
      if conactenated_string.downcase.scan(t).length > @@tmplength
        
      
        @@tmplength = conactenated_string.downcase.scan(t).length
       
        @@final_string = t

      end
      
    end

    

  end

  return @@final_string
  
end

Instance Method Details

#get_all_pluginsObject

get_all_plugins : public

The method is responsible for defining all existing modules in current application

returns modules as an array


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/generators/mcms_authentication/templates/models.rb', line 52

def get_all_plugins

  plugin = []
  
  i = 0
  
  get_models.each do |model|

    unless get_relations(model).empty?
      
      plugin[i] =  get_relations(model) << model
      
      i = i + 1
    end
  end

  plugin = manipulated_array(plugin)

  plugin = manipulated_array(plugin)

  return plugin
  
end

#get_modelsObject

@Params : Array

@Returns                              : Array
@Purpose                              : Returns array of existing models


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/generators/mcms_authentication/templates/models.rb', line 20

def get_models
  
  models = []
 
  # @models = Dir[MODEL_DIR].map {|f| File.basename(f, '.*').camelize.constantize.name } # getting existing models
   Rails.application.eager_load!

  @models = ActiveRecord::Base.descendants.collect(&:name)

  @models.reject!{|m| m.constantize.superclass != ActiveRecord::Base } #removing non models
  
  @models.each do |model|
    
    models << model
    
  end

  models.uniq # returning as array
  
end

#get_relations(model) ⇒ Object

@Params : Array

@Returns                              : Array
@Purpose                              : Returns array of interrelated models


106
107
108
109
110
111
112
113
# File 'lib/generators/mcms_authentication/templates/models.rb', line 106

def get_relations model

  #the following line of code is actually deciding the module of the particular model associated
  
  m = model.constantize.reflect_on_all_associations.map{|mac| mac.class_name if mac.macro==:has_many || mac.macro == :has_and_belongs_to_many}.compact
 
  m
end

#manipulated_array(arr) ⇒ Object

@Params : Array

@Returns                              : Array
@Purpose                              : Returns array of arrays which are unique and compact


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/generators/mcms_authentication/templates/models.rb', line 80

def manipulated_array(arr)

  for i in 0..arr.length - 1
    for j in (i+1)..arr.length - 1
      unless arr[j].nil?

        if((arr[i]&arr[j]).any?)

          tmp = (arr[i] + arr[j]).uniq
          arr.delete(arr[j])
          arr[i] = tmp

        end

      end
    end

  end

  return arr
end