Class: ValidationBuilder
- Inherits:
-
Object
- Object
- ValidationBuilder
- Defined in:
- lib/validation_sync/validation_builder.rb
Instance Attribute Summary collapse
-
#method ⇒ Object
allows us to set default arguments for the builder.
-
#object_name ⇒ Object
allows us to set default arguments for the builder.
Instance Method Summary collapse
-
#check_model_validation(object_name = self.object_name, method = self.method, validation) ⇒ Object
Check to see if a given model attribute uses a given validation returns true / false.
-
#find_all_models ⇒ Object
Create a list of all of the current app’s Active Record Models returns an array of hashes.
-
#get_model_validation(object_name = self.object_name, method = self.method, validation) ⇒ Object
Check to see if a given model attribute uses a given validation returns either a validation object or nil.
-
#get_validation_attr(object_name = self.object_name, method = self.method, validation, option) ⇒ Object
Fetch a given option for a model’s attribute’s validation returns the specified validation option.
-
#get_validation_message(object_name = self.object_name, method = self.method, validation) ⇒ Object
Fetch the validation message for a given model’s attribute’s validation returns a string.
Instance Attribute Details
#method ⇒ Object
allows us to set default arguments for the builder
4 5 6 |
# File 'lib/validation_sync/validation_builder.rb', line 4 def method @method end |
#object_name ⇒ Object
allows us to set default arguments for the builder
4 5 6 |
# File 'lib/validation_sync/validation_builder.rb', line 4 def object_name @object_name end |
Instance Method Details
#check_model_validation(object_name = self.object_name, method = self.method, validation) ⇒ Object
Check to see if a given model attribute uses a given validation returns true / false
53 54 55 56 57 58 59 |
# File 'lib/validation_sync/validation_builder.rb', line 53 def check_model_validation(object_name=self.object_name, method=self.method, validation) if get_model_validation(object_name, method, validation).nil? return false else return true end end |
#find_all_models ⇒ Object
Create a list of all of the current app’s Active Record Models returns an array of hashes
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/validation_sync/validation_builder.rb', line 11 def find_all_models models = [] # iterate over all files in folder folder = File.join(ValidationSync::Engine::APP_ROOT, "app", "models") Dir[File.join(folder, "*")].each do |filename| klass = File.basename(filename, '.rb').camelize.constantize next unless klass.ancestors.include?(ActiveRecord::Base) model = { name: klass.name.downcase, attributes: klass.new.attributes.keys } models << model end return models end |
#get_model_validation(object_name = self.object_name, method = self.method, validation) ⇒ Object
Check to see if a given model attribute uses a given validation returns either a validation object or nil
38 39 40 41 42 43 44 45 |
# File 'lib/validation_sync/validation_builder.rb', line 38 def get_model_validation(object_name=self.object_name, method=self.method, validation) object_class = object_name.camelize.constantize if val = object_class.validators_on(method).find { |v| v.class == validation } val else nil end end |
#get_validation_attr(object_name = self.object_name, method = self.method, validation, option) ⇒ Object
Fetch a given option for a model’s attribute’s validation returns the specified validation option
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/validation_sync/validation_builder.rb', line 67 def get_validation_attr(object_name=self.object_name, method=self.method, validation, option) # Make sure that we use this validation for this attribute if get_model_validation(object_name, method, validation) # look up the option object_class = object_name.camelize.constantize validation = object_class.validators_on(method).find { |v| v.class == validation } return (validation.[option]) ? validation.[option] : nil else nil end end |
#get_validation_message(object_name = self.object_name, method = self.method, validation) ⇒ Object
Fetch the validation message for a given model’s attribute’s validation returns a string
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/validation_sync/validation_builder.rb', line 88 def (object_name=self.object_name, method=self.method, validation) # Are we even using this validation? if (val = get_model_validation(object_name, method, validation)).nil? nil else # we are using the validation, now find the message if val.[:message] # Simple lookup: return val.[:message] else # Takes some digging: # get the index of this validation in the validators_on array... validators_array = [] object_class = object_name.camelize.constantize object_class.validators_on(method).each do |v| validators_array << v.class end validators_hash = Hash[validators_array.map.with_index.to_a] index = validators_hash[validation] # create a new object object = object_class.new # Do we need ro define a confirmation_of attribute? # If so, then we should read the errors from this # attribute, rather than the original if object_class.method_defined?("#{method}_confirmation") object.send("#{method}_confirmation=", "#{object.send(method)}_not_confirmed") method_to_read_errors_from = "#{method}_confirmation".to_sym else method_to_read_errors_from = method end # gather the errors object.valid? # and match the index to the errors array if object.errors.[method_to_read_errors_from] return object.errors.[method_to_read_errors_from][index] end end end end |