Class: Upmin::Klass
- Inherits:
-
Object
- Object
- Upmin::Klass
- Defined in:
- lib/upmin/klass.rb
Instance Attribute Summary collapse
-
#color ⇒ Object
Returns the value of attribute color.
-
#model ⇒ Object
Returns the value of attribute model.
Class Method Summary collapse
-
.all ⇒ Object
Returns an array of all Klass instances.
- .colors ⇒ Object
-
.find(model) ⇒ Object
Takes a Rails ActiveRecord or the name of one and returns an Upmin::Klass instance of the model.
- .models ⇒ Object
Instance Method Summary collapse
-
#actions ⇒ Object
Returns all of the upmin actions for the ActiveRecord model referenced by this Klass object.
-
#association_type(assoc_name) ⇒ Object
Tries to find an association type based on the reflection.
-
#associations ⇒ Object
Returns all associations that are not used in through associations eg - an Order’s products, but not an order’s product_orders that link the two.
-
#attribute_type(attr_name) ⇒ Object
Returns the type for an attribute by checking the columns hash.
-
#attributes ⇒ Object
Returns all of the upmin attributes for the ActiveRecord model referenced by this Klass object.
-
#find(*args) ⇒ Object
Exposing a model method, but wrapping the result in an Upmin::Model.
-
#humanized_name(type = :plural) ⇒ Object
Returns the class name, split at camelCase, with the last word pluralized if it is plural.
-
#initialize(model, options = {}) ⇒ Klass
constructor
A new instance of Klass.
-
#name ⇒ Object
Returns the class name, capitalized as it would be with User.name or OrderShipment.name - “User”, or “OrderShipment”.
- #path_hash ⇒ Object
- #plural_associations ⇒ Object
- #ransack(*args) ⇒ Object
- #reflections ⇒ Object
Constructor Details
#initialize(model, options = {}) ⇒ Klass
Returns a new instance of Klass.
7 8 9 10 11 12 13 |
# File 'lib/upmin/klass.rb', line 7 def initialize(model, = {}) self.model = model if [:color] self.color = [:color] end end |
Instance Attribute Details
#color ⇒ Object
Returns the value of attribute color.
5 6 7 |
# File 'lib/upmin/klass.rb', line 5 def color @color end |
#model ⇒ Object
Returns the value of attribute model.
4 5 6 |
# File 'lib/upmin/klass.rb', line 4 def model @model end |
Class Method Details
.all ⇒ Object
Returns an array of all Klass instances
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/upmin/klass.rb', line 128 def Klass.all return @all if defined?(@all) all = [] models.each_with_index do |model, i| klass = Klass.new(model, color: colors[i % colors.length]) all << klass end return @all = all end |
.colors ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/upmin/klass.rb', line 140 def Klass.colors return [ :light_blue, :blue_green, :red, :yellow, :orange, :purple, :dark_blue, :dark_red, :green ] end |
.find(model) ⇒ Object
Takes a Rails ActiveRecord or the name of one and returns an Upmin::Klass instance of the model.
123 124 125 |
# File 'lib/upmin/klass.rb', line 123 def Klass.find(model) return all.select{|k| k.name == model.to_s}.first end |
.models ⇒ Object
154 155 156 157 158 159 160 161 162 |
# File 'lib/upmin/klass.rb', line 154 def Klass.models # If Rails ::Rails.application.eager_load! rails_models = ::ActiveRecord::Base.descendants.select do |m| m.to_s != "ActiveRecord::SchemaMigration" end return rails_models end |
Instance Method Details
#actions ⇒ Object
Returns all of the upmin actions for the ActiveRecord model referenced by this Klass object.
48 49 50 |
# File 'lib/upmin/klass.rb', line 48 def actions return model.upmin_actions end |
#association_type(assoc_name) ⇒ Object
Tries to find an association type based on the reflection
72 73 74 75 76 77 78 79 80 |
# File 'lib/upmin/klass.rb', line 72 def association_type(assoc_name) reflection = reflections.select { |r| r.name == assoc_name.to_sym }.first if reflection return reflection.foreign_type.to_s.gsub(/_type$/, "").pluralize.to_sym else return :unknown end end |
#associations ⇒ Object
Returns all associations that are not used in through associations eg - an Order’s products, but not an order’s product_orders that link the two.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/upmin/klass.rb', line 54 def associations return @associations if defined?(@associations) all = [] ignored = [] model.reflect_on_all_associations.each do |reflection| all << reflection.name.to_sym # We need to remove the ignored later because we don't know the order they come in. if reflection.is_a?(::ActiveRecord::Reflection::ThroughReflection) ignored << reflection.[:through] end end return @associations = all - ignored end |
#attribute_type(attr_name) ⇒ Object
Returns the type for an attribute by checking the columns hash. If no match can be found, :unknown is returned. NOTE - the Upmin::Model version of this will look at the actual contents of the attr_name if :unknown is returned, so this version is more accurate if you can use it.
37 38 39 40 41 42 43 |
# File 'lib/upmin/klass.rb', line 37 def attribute_type(attr_name) if connection_adapter = model.columns_hash[attr_name.to_s] return connection_adapter.type else return :unknown end end |
#attributes ⇒ Object
Returns all of the upmin attributes for the ActiveRecord model referenced by this Klass object.
28 29 30 |
# File 'lib/upmin/klass.rb', line 28 def attributes return model.upmin_attributes end |
#find(*args) ⇒ Object
Exposing a model method, but wrapping the result in an Upmin::Model
17 18 19 |
# File 'lib/upmin/klass.rb', line 17 def find(*args) return Upmin::Model.new(model.find(*args)) end |
#humanized_name(type = :plural) ⇒ Object
Returns the class name, split at camelCase, with the last word pluralized if it is plural.
98 99 100 101 102 103 104 |
# File 'lib/upmin/klass.rb', line 98 def humanized_name(type = :plural) names = model.name.split(/(?=[A-Z])/) if type == :plural names[names.length-1] = names.last.pluralize end return names.join(" ") end |
#name ⇒ Object
Returns the class name, capitalized as it would be with User.name or OrderShipment.name - “User”, or “OrderShipment”
107 108 109 |
# File 'lib/upmin/klass.rb', line 107 def name return model.name end |
#path_hash ⇒ Object
111 112 113 114 115 |
# File 'lib/upmin/klass.rb', line 111 def path_hash return { klass: klass.name } end |
#plural_associations ⇒ Object
82 83 84 85 86 |
# File 'lib/upmin/klass.rb', line 82 def plural_associations return model.reflect_on_all_associations .select{ |r| r.collection? } .map{ |r| r.name.to_sym } end |
#ransack(*args) ⇒ Object
21 22 23 |
# File 'lib/upmin/klass.rb', line 21 def ransack(*args) return model.ransack(*args) end |
#reflections ⇒ Object
88 89 90 |
# File 'lib/upmin/klass.rb', line 88 def reflections return model.reflect_on_all_associations end |