Sha256: c744d608da287ee2c78aee9a17294a0c45e787b29fff6d95895008c7fd723e78

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

class ActiveMimic
  extend ActiveModel::Callbacks
  extend ActiveModel::Naming
  include ActiveModel::Serialization
  include ActiveAttr::Attributes
  include ActiveAttr::MassAssignment

  attribute :id
  attribute :type

  define_model_callbacks :save
  define_model_callbacks :destroy

  def self.create(attrs)
    new(attrs).tap(&:save)
  end

  def indexable_id
    "#{self.class.to_s}_#{self.id}"
  end

  def type
    self.class.to_s
  end

  def save
    self.id ||= self.class.next_id
    run_callbacks :save do
      true
    end
  end

  def destroy
    run_callbacks :destroy do
      true
    end
  end

  def self.next_id
    @next_id ||= 0
    @next_id += 1
  end

  def self.localized_attribute(name)
    attribute "#{name}_translations", type: Hash

    define_method name do
      send("#{name}_translations") && send("#{name}_translations")[I18n.locale.to_s]
    end

    define_method "#{name}=" do |value|
      send("#{name}_translations=", {}) if send("#{name}_translations").nil?
      send("#{name}_translations").merge!(I18n.locale.to_s => value)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
activesearch-0.3.2 spec/support/active_mimic.rb
activesearch-0.3.1 spec/support/active_mimic.rb
activesearch-0.3.0 spec/support/active_mimic.rb