Class: RegressionModel
- Inherits:
-
Object
- Object
- RegressionModel
- Defined in:
- lib/regression_model.rb
Instance Attribute Summary collapse
-
#model ⇒ Object
Returns the value of attribute model.
Instance Method Summary collapse
- #belong_to_relations ⇒ Object
- #database_columns ⇒ Object
- #database_indexes ⇒ Object
- #has_many_relations ⇒ Object
- #has_one_relations ⇒ Object
-
#initialize(model) ⇒ RegressionModel
constructor
A new instance of RegressionModel.
- #nested_attributes ⇒ Object
- #validators ⇒ Object
Constructor Details
#initialize(model) ⇒ RegressionModel
Returns a new instance of RegressionModel.
5 6 7 |
# File 'lib/regression_model.rb', line 5 def initialize(model) @model = model end |
Instance Attribute Details
#model ⇒ Object
Returns the value of attribute model.
3 4 5 |
# File 'lib/regression_model.rb', line 3 def model @model end |
Instance Method Details
#belong_to_relations ⇒ Object
9 10 11 12 13 |
# File 'lib/regression_model.rb', line 9 def belong_to_relations @model.constantize.reflect_on_all_associations(:belongs_to).map(&:name).map do |relation| "it { is_expected.to belong_to :#{relation}}" end.join("\n\t") rescue nil end |
#database_columns ⇒ Object
57 58 59 60 61 |
# File 'lib/regression_model.rb', line 57 def database_columns @model.constantize.columns.map(&:name).map do |column| "it { is_expected.to have_db_column :#{column} }" end.join("\n\t") rescue nil end |
#database_indexes ⇒ Object
63 64 65 66 67 |
# File 'lib/regression_model.rb', line 63 def database_indexes ActiveRecord::Base.connection.indexes(@model.tableize.gsub("/", "_")).map do |indexes| "it { is_expected.to have_db_index #{indexes.columns}}" end.flatten.join("\n\t") rescue nil end |
#has_many_relations ⇒ Object
21 22 23 24 25 |
# File 'lib/regression_model.rb', line 21 def has_many_relations @model.constantize.reflect_on_all_associations(:has_many).map(&:name).map do |relation| "it { is_expected.to have_many :#{relation}}" end.join("\n\t") rescue nil end |
#has_one_relations ⇒ Object
15 16 17 18 19 |
# File 'lib/regression_model.rb', line 15 def has_one_relations @model.constantize.reflect_on_all_associations(:has_one).map(&:name).map do |relation| "it { is_expected.to have_one :#{relation}}" end.join("\n\t") rescue nil end |
#nested_attributes ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/regression_model.rb', line 49 def nested_attributes if @model.constantize..present? @model.constantize..keys.map do |key| "it { is_expected.to accept_nested_attributes_for :#{key} }" end.join("\n\t") rescue nil end end |
#validators ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/regression_model.rb', line 27 def validators validator_specs = [] @model.constantize.validators.each do |validator| if validator.class.to_s == ActiveRecord::Validations::PresenceValidator.to_s validator.attributes.each do |attribute| validator_specs << "it { is_expected.to validate_presence_of :#{attribute} }" end end if validator.class.to_s == ActiveModel::Validations::LengthValidator.to_s validator.attributes.each do |attribute| minimum = validator.[:minimum] maximum = validator.[:maximum] validator_specs << "it { is_expected.to allow_value(Faker::Lorem.characters(#{minimum})).for :#{attribute} }" if minimum validator_specs << "it { is_expected.not_to allow_value(Faker::Lorem.characters(#{minimum - 1})).for :#{attribute} }" if minimum validator_specs << "it { is_expected.to allow_value(Faker::Lorem.characters(#{maximum})).for :#{attribute} }" if maximum validator_specs << "it { is_expected.not_to allow_value(Faker::Lorem.characters(#{maximum + 1})).for :#{attribute} }" if maximum end end end rescue [] validator_specs.compact.uniq.join("\n\t") end |