Class: Upmin::Model
- Inherits:
-
Object
- Object
- Upmin::Model
- Defined in:
- lib/upmin/model.rb
Instance Attribute Summary collapse
-
#color ⇒ Object
Returns the value of attribute color.
-
#rails_model ⇒ Object
Returns the value of attribute rails_model.
Class Method Summary collapse
-
.all ⇒ Object
TODO(jon): Store this in the future so it doesn’t have to be looked up every call.
-
.find(name) ⇒ Object
Generic Class Methods.
- .rails_models ⇒ Object
Instance Method Summary collapse
-
#attributes ⇒ Object
Methods for determinining attributes, and their types.
-
#find(id) ⇒ Object
Wrapper methods that the normal model would have access to.
- #form_name ⇒ Object (also: #partial_name)
- #get_attr_type(attr_name) ⇒ Object
-
#initialize(rails_model, options = {}) ⇒ Model
constructor
A new instance of Model.
- #name ⇒ Object
-
#perform_action(instance, method, arguments) ⇒ Object
Methods that perform actions on an instance but require some prep and logic ahead of time that shouldn’t be injected into the active record.
- #search(*args) ⇒ Object (also: #ransack)
- #to_s ⇒ Object
- #u_name ⇒ Object
- #upmin_methods ⇒ Object
Constructor Details
#initialize(rails_model, options = {}) ⇒ Model
Returns a new instance of Model.
7 8 9 10 11 12 13 |
# File 'lib/upmin/model.rb', line 7 def initialize(rails_model, = {}) self.rails_model = rails_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/model.rb', line 5 def color @color end |
#rails_model ⇒ Object
Returns the value of attribute rails_model.
4 5 6 |
# File 'lib/upmin/model.rb', line 4 def rails_model @rails_model end |
Class Method Details
.all ⇒ Object
TODO(jon): Store this in the future so it doesn’t have to be looked up every call.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/upmin/model.rb', line 98 def Model.all return @models_array if defined?(@models_array) models_array = [] colors = [ :light_blue, :blue_green, :red, :yellow, :orange, :purple, :dark_blue, :dark_red, :green ] rails_models.each_with_index do |rails_model, i| ac_model = Model.new(rails_model, color: colors[i % colors.length]) models_array << ac_model end return @models_array = models_array end |
.find(name) ⇒ Object
Generic Class Methods
93 94 95 |
# File 'lib/upmin/model.rb', line 93 def Model.find(name) return all.select{|a| a.to_s == name.to_s}.first end |
.rails_models ⇒ Object
121 122 123 124 125 126 127 128 |
# File 'lib/upmin/model.rb', line 121 def Model.rails_models ::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
#attributes ⇒ Object
Methods for determinining attributes, and their types.
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/upmin/model.rb', line 71 def attributes return @attributes if defined?(@attributes) attributes = {} rails_model.upmin_attributes.each do |u_attr| attributes[u_attr] = {} attributes[u_attr][:type] = get_attr_type(u_attr) end return @attributes = attributes end |
#find(id) ⇒ Object
Wrapper methods that the normal model would have access to
33 34 35 |
# File 'lib/upmin/model.rb', line 33 def find(id) return rails_model.find(id) end |
#form_name ⇒ Object Also known as: partial_name
27 28 29 |
# File 'lib/upmin/model.rb', line 27 def form_name return rails_model.name.underscore end |
#get_attr_type(attr_name) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/upmin/model.rb', line 83 def get_attr_type(attr_name) if uc = rails_model.columns_hash[attr_name.to_s] return uc.type else return :unknown end end |
#name ⇒ Object
19 20 21 |
# File 'lib/upmin/model.rb', line 19 def name return rails_model.name end |
#perform_action(instance, method, arguments) ⇒ Object
Methods that perform actions on an instance but require some prep and logic ahead of time that shouldn’t be injected into the active record.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/upmin/model.rb', line 48 def perform_action(instance, method, arguments) raise "Invalid method: #{method}" unless upmin_methods.include?(method.to_sym) params = instance.method(method).parameters args_to_send = [] params.each do |type, name| if type == :req raise "Missing argument: #{name}" unless arguments[name] args_to_send << arguments[name] puts "Added: #{arguments[name].inspect}" elsif type == :opt puts arguments.inspect args_to_send << arguments[name] if arguments[name] puts "Added: #{arguments[name].inspect}" else # :block or ?? next end end return instance.send(method, *args_to_send) end |
#search(*args) ⇒ Object Also known as: ransack
37 38 39 |
# File 'lib/upmin/model.rb', line 37 def search(*args) return self.rails_model.ransack(*args) end |
#to_s ⇒ Object
15 16 17 |
# File 'lib/upmin/model.rb', line 15 def to_s return rails_model.to_s end |
#u_name ⇒ Object
23 24 25 |
# File 'lib/upmin/model.rb', line 23 def u_name return rails_model.upmin_name end |
#upmin_methods ⇒ Object
42 43 44 |
# File 'lib/upmin/model.rb', line 42 def upmin_methods return self.rails_model.upmin_methods end |