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.



56
57
58
# File 'lib/conjoin/active_record.rb', line 56

def req_params
  @req_params
end

Instance Method Details

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



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
139
140
# File 'lib/conjoin/active_record.rb', line 106

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



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

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

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



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

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



80
81
82
83
84
85
# File 'lib/conjoin/active_record.rb', line 80

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



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

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



95
96
97
98
# File 'lib/conjoin/active_record.rb', line 95

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

#set_unrestricted_attributes(*fields) ⇒ Object



100
101
102
103
104
# File 'lib/conjoin/active_record.rb', line 100

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

#valid_except?(except = {}) ⇒ Boolean



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

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



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

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



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

def validate
end

#validates(req_params) ⇒ Object



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

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