Module: Conjoin::ActiveRecord::Form
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/conjoin/active_record.rb
Instance Attribute Summary collapse
-
#req_params ⇒ Object
Returns the value of attribute req_params.
Instance Method Summary collapse
- #add_creator_and_updater_for(model, current_user = nil, current_params) ⇒ Object
- #is_form? ⇒ Boolean
- #remove_error!(attribute, message = :invalid, options = {}) ⇒ Object
- #save_as(current_user) ⇒ Object
- #save_unrestricted_attributes ⇒ Object
- #set_unrestricted_attribute(field, value) ⇒ Object
- #set_unrestricted_attributes(*fields) ⇒ Object
- #valid_except?(except = {}) ⇒ Boolean
- #valid_only?(*columns) ⇒ Boolean
- #validate ⇒ Object
- #validates(req_params, opts = {}) ⇒ Object
Instance Attribute Details
#req_params ⇒ Object
Returns the value of attribute req_params.
57 58 59 |
# File 'lib/conjoin/active_record.rb', line 57 def req_params @req_params end |
Instance Method Details
#add_creator_and_updater_for(model, current_user = nil, current_params) ⇒ Object
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 138 139 140 141 142 143 144 145 |
# File 'lib/conjoin/active_record.rb', line 111 def add_creator_and_updater_for(model, current_user = nil, current_params) # set the creator and updater id = current_user.try(:id) || ENV["SYSTEM_USER_ID"] # Save creator if model.respond_to? :creator_id and model.new_record? model.set_unrestricted_attribute 'creator_id', id end # Save updater if model.respond_to? :updater_id model.set_unrestricted_attribute 'updater_id', id end return unless current_params # loop through associated records current_params.each do |name, value| if name.end_with?("_attributes") associated_name = name.gsub(/_attributes$/, '') associated_model = model.try associated_name if associated_model.kind_of? ::ActiveRecord::Base new_current_params = current_params[name] if new_current_params.kind_of? Hash add_creator_and_updater_for associated_model, current_user, new_current_params end elsif associated_model.kind_of? ActiveRecord::Associations::CollectionProxy new_current_params = current_params[name] associated_model.each_with_index do |current_model, i| add_creator_and_updater_for current_model, current_user, new_current_params[i] end end end end end |
#is_form? ⇒ Boolean
64 65 66 |
# File 'lib/conjoin/active_record.rb', line 64 def is_form? self.class.model_name.to_s[/Form$/] end |
#remove_error!(attribute, message = :invalid, options = {}) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/conjoin/active_record.rb', line 147 def remove_error!(attribute, = :invalid, = {}) # -- Same code as private method ActiveModel::Errors.normalize_message(attribute, message, options). = [:if, :unless, :on, :allow_nil, :allow_blank, :strict] case when Symbol = self.errors.(attribute, , .except(*)) when Proc = .call else = end # -- end block # -- Delete message - based on ActiveModel::Errors.added?(attribute, message = :invalid, options = {}). = self.errors[attribute].delete() rescue nil # -- Delete attribute from errors if message array is empty. self.errors..delete(attribute) if !self.errors.[attribute].present? return end |
#save_as(current_user) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/conjoin/active_record.rb', line 85 def save_as current_user run_callbacks :save_as do add_creator_and_updater_for self, current_user, req_params save! end end |
#save_unrestricted_attributes ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/conjoin/active_record.rb', line 92 def save_unrestricted_attributes if @unrestricted_attributes and @unrestricted_attributes.any? @unrestricted_attributes.each do |field, value| self.send "#{field}=", value end end end |
#set_unrestricted_attribute(field, value) ⇒ Object
100 101 102 103 |
# File 'lib/conjoin/active_record.rb', line 100 def set_unrestricted_attribute field, value @unrestricted_attributes ||= {} @unrestricted_attributes[field] = value end |
#set_unrestricted_attributes(*fields) ⇒ Object
105 106 107 108 109 |
# File 'lib/conjoin/active_record.rb', line 105 def set_unrestricted_attributes *fields fields..each do |field, value| set_unrestricted_attribute field, value end end |
#valid_except?(except = {}) ⇒ Boolean
167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/conjoin/active_record.rb', line 167 def valid_except?(except={}) self.valid? # -- Use this to call valid? for superclass if self.valid? is overridden. # self.class.superclass.instance_method(:valid?).bind(self).call except.each do |attribute, | if .present? remove_error!(attribute, ) else self.errors.delete(attribute) end end !self.errors.present? end |
#valid_only?(*columns) ⇒ Boolean
181 182 183 184 185 186 187 |
# File 'lib/conjoin/active_record.rb', line 181 def valid_only? *columns self.valid? self.errors..each do |field, | self.errors.delete(field) unless columns.include? field end !self.errors.present? end |
#validate ⇒ Object
68 69 |
# File 'lib/conjoin/active_record.rb', line 68 def validate end |
#validates(req_params, opts = {}) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/conjoin/active_record.rb', line 71 def validates req_params, opts = {} req_params = req_params.is_a?(OpenStruct) ? req_params.to_hash : HashIndifferent.new(req_params) @req_params = req_params if as = opts.delete(:as) add_creator_and_updater_for self, as, req_params end run_callbacks :validates do self.attributes = req_params valid? end end |