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_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

Returns:

  • (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, 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



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.extract_options!.each do |field, value|
    set_unrestricted_attribute field, value
  end
end

#valid_except?(except = {}) ⇒ Boolean

Returns:

  • (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, 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)


181
182
183
184
185
186
187
# File 'lib/conjoin/active_record.rb', line 181

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

#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