Sha256: abbc5ab7c4b79745e6df965d1cff2913bffb925225e04fdcc7ab8edfa704a299

Contents?: true

Size: 884 Bytes

Versions: 1

Compression:

Stored size: 884 Bytes

Contents

module ChosenTemplate
  module Chooser

    extend ActiveSupport::Concern

    included do

      self.template_choices.each do |template_choice|
        [:previewed, :published].each do |action_type|
          method_name = :"#{action_type}_#{template_choice.to_s.singularize}"
          define_method(method_name) do
            order_scope_name = :"by_template_#{action_type}_at"
            template_class = template_choice.to_s.classify.constantize
            foreign_key = "#{self.class.name.underscore}_id"
            template_scope = if template_class.column_names.include?(foreign_key)
                               self.send(template_choice)
                             else
                               template_class.all
                             end
            template_scope.send(order_scope_name).first
          end
        end
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chosen_template-1.0.0 lib/chosen_template/chooser.rb