Sha256: 10994e60a521c218804668432a980504a51336c42ac004afa91df91c1d12d996

Contents?: true

Size: 1.95 KB

Versions: 7

Compression:

Stored size: 1.95 KB

Contents

# Copyright (c) 2008-2013 Michael Dvorkin and contributors.
#
# Fat Free CRM is freely distributable under the terms of MIT license.
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------
class CustomFieldPair < CustomField
  has_one :pair, class_name: CustomFieldPair, foreign_key: 'pair_id', dependent: :destroy # points to 'end'

  # Helper to create a pair. Used in fields_controller
  #------------------------------------------------------------------------------
  def self.create_pair(params)
    fields = params['field']
    as = params['field']['as']
    pair = params.delete('pair')
    base_params = fields.delete_if { |k, _v| !%w(field_group_id label as).include?(k) }
    klass = ("custom_field_" + as.gsub('pair', '_pair')).classify.constantize
    field1 = klass.create(base_params.merge(pair['0']))
    field2 = klass.create(base_params.merge(pair['1']).merge('pair_id' => field1.id, 'required' => field1.required, 'disabled' => field1.disabled))
    [field1, field2]
  end

  # Helper to update a pair. Used in fields_controller
  #------------------------------------------------------------------------------
  def self.update_pair(params)
    fields = params['field']
    pair = params.delete('pair')
    base_params = fields.delete_if { |k, _v| !%w(field_group_id label as).include?(k) }
    field1 = CustomFieldPair.find(params['id'])
    field1.update_attributes(base_params.merge(pair['0']))
    field2 = field1.paired_with
    field2.update_attributes(base_params.merge(pair['1']).merge('required' => field1.required, 'disabled' => field1.disabled))
    [field1, field2]
  end

  # Returns the field that this field is paired with
  #------------------------------------------------------------------------------
  def paired_with
    pair || CustomFieldPair.where(pair_id: id).first
  end

  ActiveSupport.run_load_hooks(:fat_free_crm_custom_field_pair, self)
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
fat_free_crm-0.14.2 app/models/fields/custom_field_pair.rb
fat_free_crm-0.14.1 app/models/fields/custom_field_pair.rb
fat_free_crm-0.15.0.beta.2 app/models/fields/custom_field_pair.rb
fat_free_crm-0.15.0.beta app/models/fields/custom_field_pair.rb
fat_free_crm-0.14.0 app/models/fields/custom_field_pair.rb
reduced_fat_crm-0.15.0.beta app/models/fields/custom_field_pair.rb
reduced_fat_crm-0.14.0 app/models/fields/custom_field_pair.rb