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.



54
55
56
# File 'lib/conjoin/active_record.rb', line 54

def req_params
  @req_params
end

Instance Method Details

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



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
138
# File 'lib/conjoin/active_record.rb', line 104

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



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

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

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



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/conjoin/active_record.rb', line 140

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



78
79
80
81
82
83
# File 'lib/conjoin/active_record.rb', line 78

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



85
86
87
88
89
90
91
# File 'lib/conjoin/active_record.rb', line 85

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



93
94
95
96
# File 'lib/conjoin/active_record.rb', line 93

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

#set_unrestricted_attributes(*fields) ⇒ Object



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

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

#valid_except?(except = {}) ⇒ Boolean



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/conjoin/active_record.rb', line 160

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



174
175
176
177
178
179
180
# File 'lib/conjoin/active_record.rb', line 174

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



65
66
# File 'lib/conjoin/active_record.rb', line 65

def validate
end

#validates(req_params) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/conjoin/active_record.rb', line 68

def validates req_params
  req_params = req_params.is_a?(OpenStruct) ? req_params.to_hash : HashIndifferent.new(req_params)
  @req_params = req_params

  run_callbacks :validates do
    self.attributes = req_params
    valid?
  end
end