acts_as_word_cloud 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,87 +0,0 @@
1
- require 'find'
2
- class ModelMethodsHelper
3
-
4
- # finds or makes a model based on the given attributes
5
- # @param [Constant] model_name The name of the model
6
- # @param [Hash] attributes The attributes to find the model or add to the new one
7
- def self.find_or_make_by_attributes(model_name, attributes)
8
- instances = model_name.where(attributes)
9
- if instances.empty?
10
- return model_name.make(attributes)
11
- else
12
- return instances.first
13
- end
14
- end
15
-
16
- # Uses the descendants_of method to determine the model structure
17
- # Then tableizes and converts to symbol
18
- #
19
- # Load all models must be run for accurate descendants list
20
- # it is now being called every time this method is called
21
- # for now it works because this is only called one time when the authorization config is loaded on initialize
22
- # if this ever changes, this code may need to be update (2-15-2012)
23
- #
24
- # @return [Array] Array with file names underscored and pluralized in the format [:users, :case_placements, :roles, :job_locations, ... ]
25
- def self.get_model_symbol_array
26
- # Old method:
27
- # Major redesign of this method that finds namespaced models
28
- # Finds all files in the given folder, tries to find the ruby files
29
- # Then processes the names to get the names we need for the code
30
- #
31
- # @return [Array] Array with file names underscored and pluralized in the format [:users, :case_placements, :roles, :job_locations, ... ]
32
- #model_array = []
33
- #Find.find("#{Rails.root.to_s}/app/models") do |model_path|
34
- # if model_path.match(/\.rb$/)
35
- # model_path = model_path.gsub("#{Rails.root.to_s}/app/models/", "")
36
- # model_path = model_path.gsub("/","_")
37
- # model_path = model_path.gsub(/\.rb$/, "")
38
- # model_array.push model_path.pluralize.to_sym
39
- # end
40
- #end
41
- #return model_array
42
-
43
- # new method
44
- self.load_all_models
45
- # returns the table name if there is one or tableizes the model name if not
46
- # our permissions system generally uses table names for model permissions
47
- return self.descendants_of(ActiveRecord::Base).map{ |m| m.abstract_class ? m.to_s.tableize.to_sym : m.table_name.to_sym }
48
- end
49
-
50
- # Requires all models so that they are visible in the object space
51
- # This is only necessary when we are not caching classes
52
- def self.load_all_models
53
- if !::Rails.application.config.cache_classes
54
- Dir[Rails.root.join("app/models/**/*.rb")].each {|f| require f}
55
- end
56
- end
57
-
58
- # Return a list of descendants of the given model.
59
- #
60
- # If direct is set to true, only return immediate descendants
61
- # The load_all_models must be called before this is called or the result may not be correct
62
- #
63
- # @param [Constant] parent_class The parent class to look for descendants for
64
- # @param [Boolean] direct Whether to look for all descendants or immediate descendants
65
- # @return [Array] An array of class constants that satisfy the conditions of the parameters
66
- def self.descendants_of(parent_class, direct = false)
67
- classes = []
68
- ObjectSpace.each_object(::Class).each do |klass|
69
- if direct
70
- classes << klass if klass.superclass == parent_class
71
- else
72
- classes << klass if klass < parent_class
73
- end
74
- end
75
- return classes
76
- end
77
-
78
- # Convenience method to return the results of descendants_of with direct set to true
79
- #
80
- # The load_all_models must be called before this is called or the result may not be correct
81
- #
82
- # @param [Constant] parent_class The parent class to look for descendants for
83
- # @return [Array] An array of class constants that satisfy the conditions of the parameters
84
- def self.direct_descendants_of(parent_class)
85
- return self.descendants_of(parent_class, true)
86
- end
87
- end