Sha256: 4aead368e31ce15cd7215f8e906a2723be562a2adc07b910d858c117d27b4dff

Contents?: true

Size: 795 Bytes

Versions: 19

Compression:

Stored size: 795 Bytes

Contents

class UserFieldType < FieldType
  attr_accessor :user_id

  validates :user_id, presence: true, if: :validate_presence?
  validate :valid_user_id?

  def data=(data_hash)
    @user_id = data_hash.deep_symbolize_keys[:user_id]
  end

  def field_item_as_indexed_json_for_field_type(field_item, options = {})
    json = {}
    json[mapping_field_name] = field_item.data['user_id']
    json
  end

  def mapping
    {name: mapping_field_name, type: :string, analyzer: :snowball}
  end

  private

  def mapping_field_name
    "#{field_name.parameterize('_')}_user"
  end

  def valid_user_id?
    return true if user_id.nil?

    unless User.exists?(user_id)
      errors.add(:user_id, "does not exist")
      false
    end
  end

  def validate_presence?
    @validations.key? :presence
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
cortex-plugins-core-0.12.3 app/models/user_field_type.rb
cortex-plugins-core-0.12.2 app/models/user_field_type.rb
cortex-plugins-core-0.12.1 app/models/user_field_type.rb
cortex-plugins-core-0.12.0 app/models/user_field_type.rb
cortex-plugins-core-0.11.3 app/models/user_field_type.rb
cortex-plugins-core-0.11.2 app/models/user_field_type.rb
cortex-plugins-core-0.11.1 app/models/user_field_type.rb
cortex-plugins-core-0.11.0 app/models/user_field_type.rb
cortex-plugins-core-0.10.4 app/models/user_field_type.rb
cortex-plugins-core-0.10.2 app/models/user_field_type.rb
cortex-plugins-core-0.10.1 app/models/user_field_type.rb
cortex-plugins-core-0.10.0 app/models/user_field_type.rb
cortex-plugins-core-0.9.1 app/models/user_field_type.rb
cortex-plugins-core-0.9.0 app/models/user_field_type.rb
cortex-plugins-core-0.8.0 app/models/user_field_type.rb
cortex-plugins-core-0.7.2 app/models/user_field_type.rb
cortex-plugins-core-0.7.1 app/models/user_field_type.rb
cortex-plugins-core-0.7.0 app/models/user_field_type.rb
cortex-plugins-core-0.6.0 app/models/user_field_type.rb