Module: Conjoin::ActiveRecord::Form

Extended by:
ActiveSupport::Concern
Defined in:
lib/conjoin/active_record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#req_paramsObject

Returns the value of attribute req_params.



61
62
63
# File 'lib/conjoin/active_record.rb', line 61

def req_params
  @req_params
end

Instance Method Details

#add_creator_and_updater_for(model, current_user = nil, current_params) ⇒ Object



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
146
147
148
149
150
151
# File 'lib/conjoin/active_record.rb', line 115

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|
    name = name.to_s
    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.to_sym] || 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.to_sym] || current_params[name])
        associated_model.each_with_index do |current_model, i|
          new_current_params ||= []
          add_creator_and_updater_for current_model, current_user, new_current_params[i]
        end
      end
    end
  end
end

#is_form?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/conjoin/active_record.rb', line 68

def is_form?
  self.class.model_name.to_s[/Form$/]
end

#remove_error!(attribute, message = :invalid, options = {}) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/conjoin/active_record.rb', line 153

def remove_error!(attribute, message = :invalid, options = {})
  # -- Same code as private method ActiveModel::Errors.normalize_message(attribute, message, options).
  callbacks_options = [:if, :unless, :on, :allow_nil, :allow_blank, :strict]
  case message
  when Symbol
    message = self.errors.generate_message(attribute, message, options.except(*callbacks_options))
  when Proc
    message = message.call
  else
    message = message
  end
  # -- end block

  # -- Delete message - based on ActiveModel::Errors.added?(attribute, message = :invalid, options = {}).
  message = self.errors[attribute].delete(message) rescue nil
  # -- Delete attribute from errors if message array is empty.
  self.errors.messages.delete(attribute) if !self.errors.messages[attribute].present?
  return message
end

#save_as(current_user) ⇒ Object



89
90
91
92
93
94
# File 'lib/conjoin/active_record.rb', line 89

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_attributesObject



96
97
98
99
100
101
102
# File 'lib/conjoin/active_record.rb', line 96

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



104
105
106
107
# File 'lib/conjoin/active_record.rb', line 104

def set_unrestricted_attribute field, value
  @unrestricted_attributes ||= {}
  @unrestricted_attributes[field] = value
end

#set_unrestricted_attributes(*fields) ⇒ Object



109
110
111
112
113
# File 'lib/conjoin/active_record.rb', line 109

def set_unrestricted_attributes *fields
  fields.extract_options!.each do |field, value|
    set_unrestricted_attribute field, value
  end
end

#valid_except?(except = {}) ⇒ Boolean

Returns:

  • (Boolean)


173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/conjoin/active_record.rb', line 173

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, message|
    if message.present?
      remove_error!(attribute, message)
    else
      self.errors.delete(attribute)
    end
  end
  !self.errors.present?
end

#valid_only?(*columns) ⇒ Boolean

Returns:

  • (Boolean)


187
188
189
190
191
192
193
# File 'lib/conjoin/active_record.rb', line 187

def valid_only? *columns
  self.valid?
  self.errors.messages.each do |field, message|
    self.errors.delete(field) unless columns.include? field
  end
  !self.errors.present?
end

#validateObject



72
73
# File 'lib/conjoin/active_record.rb', line 72

def validate
end

#validates(req_params, opts = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/conjoin/active_record.rb', line 75

def validates req_params, opts = {}
  req_params  = req_params.to_h
  @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