Module: DynamicModels
- Included in:
- ActionController::Base
- Defined in:
- lib/dynamic_models.rb
Instance Method Summary collapse
- #base_model_class ⇒ Object
-
#base_model_class_name ⇒ Object
the model class, inferred from the controller.
-
#fetch_model ⇒ Object
returns a model using the id from the params.
-
#fetch_model_list ⇒ Object
returns an array of models (using the name of this controller).
-
#model_class ⇒ Object
the class we are working with, if an STI model then it will fail loudly on a type which inst descendant from the class which corresponds to this controller.
-
#model_name ⇒ Object
model name from the controller or type parameter (for a model which is using STI).
-
#new_model(defaults = {}) ⇒ Object
returns a new model, it can be set with an optional hash.
-
#parent_model ⇒ Object
looks for object_id notation, and returns a new model.
-
#plural_model_name ⇒ Object
plural form of the model name from the controller.
-
#sti_model? ⇒ Boolean
if we are using a type parameter, then we are dealing with an STI model.
Instance Method Details
#base_model_class ⇒ Object
30 31 32 |
# File 'lib/dynamic_models.rb', line 30 def base_model_class base_model_class_name.camelize.constantize end |
#base_model_class_name ⇒ Object
the model class, inferred from the controller
26 27 28 |
# File 'lib/dynamic_models.rb', line 26 def base_model_class_name params[:controller].split('/').last.singularize end |
#fetch_model ⇒ Object
returns a model using the id from the params
60 61 62 |
# File 'lib/dynamic_models.rb', line 60 def fetch_model model_class.find params[:id] end |
#fetch_model_list ⇒ Object
returns an array of models (using the name of this controller)
65 66 67 68 69 70 71 |
# File 'lib/dynamic_models.rb', line 65 def fetch_model_list if parent_model return parent_model.send("#{model_name.pluralize.downcase}") else return model_class.find(:all) end end |
#model_class ⇒ Object
the class we are working with, if an STI model then it will fail loudly on a type which inst descendant from the class which corresponds to this controller
35 36 37 38 39 40 41 |
# File 'lib/dynamic_models.rb', line 35 def model_class klass = model_name.camelize.constantize if sti_model? raise "you can only pass a type which descends from #{params[:controller]}" unless klass.sti_model? and klass.superclass == base_model_class end klass end |
#model_name ⇒ Object
model name from the controller or type parameter (for a model which is using STI)
21 22 23 |
# File 'lib/dynamic_models.rb', line 21 def model_name sti_model? ? params[:type].underscore : base_model_class_name end |
#new_model(defaults = {}) ⇒ Object
returns a new model, it can be set with an optional hash
49 50 51 52 53 54 55 56 57 |
# File 'lib/dynamic_models.rb', line 49 def new_model(defaults = {}) new_model = model_class.new(defaults) # if there is a parent then associate it with the model if parent_model new_model.send("#{parent_model.class.name.underscore}=", parent_model) end # return the new model new_model end |
#parent_model ⇒ Object
looks for object_id notation, and returns a new model
6 7 8 9 10 11 12 13 |
# File 'lib/dynamic_models.rb', line 6 def parent_model params.each do |name, value| if name =~ /(.+)_id$/ return @parent_model ||= $1.camelize.constantize.find(value) end end nil end |
#plural_model_name ⇒ Object
plural form of the model name from the controller
44 45 46 |
# File 'lib/dynamic_models.rb', line 44 def plural_model_name params[:controller].split('/').last end |
#sti_model? ⇒ Boolean
if we are using a type parameter, then we are dealing with an STI model
16 17 18 |
# File 'lib/dynamic_models.rb', line 16 def sti_model? params[:type].present? end |