Sha256: c787a175e895c204c0a070cda842d7043e12fc38ff44336661c6182536725a5f

Contents?: true

Size: 846 Bytes

Versions: 2

Compression:

Stored size: 846 Bytes

Contents

module ChosenTemplate
  module ActiveRecordExtensions

    CHOOSABLE_REQUIRED_COLUMNS = [:template_published_at, :template_previewed_at]
    extend ActiveSupport::Concern

    module ClassMethods
      def chooses_templates(*template_choices)
        cattr_accessor :template_choices
        self.template_choices = template_choices
        include Chooser
      end

      def choosable_template(options={})
        cattr_accessor :chosen_by
        self.chosen_by = options[:by]

        CHOOSABLE_REQUIRED_COLUMNS.each do |column_name|
          unless self.column_names.include?(column_name.to_s)
            raise ArgumentError, "`#{self.table_name}` requires the `#{column_name}` column"
          end
        end
        include Chosen
      end
    end

  end
end

ActiveRecord::Base.send :include, ChosenTemplate::ActiveRecordExtensions

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
chosen_template-1.0.0 lib/chosen_template/active_record_extensions.rb
chosen_template-0.1.0 lib/chosen_template/active_record_extensions.rb