Sha256: 869289f0967b7a9b3d3dd33836a40656284b1ca7999c5678dfa93427e6670351

Contents?: true

Size: 1.96 KB

Versions: 11

Compression:

Stored size: 1.96 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

11 entries across 11 versions & 1 rubygems

Version Path
fat_free_crm-0.13.6 app/models/fields/custom_field_pair.rb
fat_free_crm-0.13.5 app/models/fields/custom_field_pair.rb
fat_free_crm-0.13.4 app/models/fields/custom_field_pair.rb
fat_free_crm-0.13.3 app/models/fields/custom_field_pair.rb
fat_free_crm-0.13.2 app/models/fields/custom_field_pair.rb
fat_free_crm-0.12.3 app/models/fields/custom_field_pair.rb
fat_free_crm-0.12.2 app/models/fields/custom_field_pair.rb
fat_free_crm-0.13.1 app/models/fields/custom_field_pair.rb
fat_free_crm-0.12.1 app/models/fields/custom_field_pair.rb
fat_free_crm-0.13.0 app/models/fields/custom_field_pair.rb
fat_free_crm-0.12.0 app/models/fields/custom_field_pair.rb