Sha256: 5cd37c5ff91edcfc5ee9890f2a901c27a82254c3840c8686fb28ff55c935fd76

Contents?: true

Size: 1.1 KB

Versions: 64

Compression:

Stored size: 1.1 KB

Contents

module Tenon
  module TenonContent
    def self.included(base)
      base.extend(ClassMethods)
    end

    module ClassMethods
      def tenon_content(content_field, i18n: false)
        Tenon::TenonContentBuilder.add_assoc(self, content_field)

        define_method("#{content_field}_i18n?") { i18n }
        if i18n && Tenon.config.languages
          Tenon.config.languages.each do |title, lang|
            Tenon::TenonContentBuilder.add_assoc(self, "#{content_field}_#{lang}")
          end
        end
      end
    end
  end

  class TenonContentBuilder
    class << self
      def add_assoc(klass, content_field)
        assoc = "#{content_field}_tenon_content_rows".to_sym
        args = [assoc, -> { where(item_method: content_field) }, assoc_opts]
        klass.send(:has_many, *args)
        klass.send(:accepts_nested_attributes_for, assoc, allow_destroy: true)
      end

      private

      def assoc_opts
        {
          class_name: 'Tenon::TenonContent::Row',
          as: :item,
          dependent: :destroy
        }
      end
    end
  end
end

ActiveRecord::Base.send(:include, Tenon::TenonContent)

Version data entries

64 entries across 64 versions & 1 rubygems

Version Path
tenon-1.1.8 lib/tenon/tenon_content.rb
tenon-1.1.7 lib/tenon/tenon_content.rb
tenon-1.1.6 lib/tenon/tenon_content.rb
tenon-2.1.0 lib/tenon/tenon_content.rb
tenon-2.0.8 lib/tenon/tenon_content.rb
tenon-2.0.7 lib/tenon/tenon_content.rb
tenon-2.0.6 lib/tenon/tenon_content.rb
tenon-2.0.5 lib/tenon/tenon_content.rb
tenon-2.0.4 lib/tenon/tenon_content.rb
tenon-2.0.3 lib/tenon/tenon_content.rb
tenon-2.0.2 lib/tenon/tenon_content.rb
tenon-2.0.1 lib/tenon/tenon_content.rb
tenon-2.0.0 lib/tenon/tenon_content.rb
tenon-1.1.5 lib/tenon/tenon_content.rb
tenon-1.1.4 lib/tenon/tenon_content.rb
tenon-1.1.3 lib/tenon/tenon_content.rb
tenon-1.1.2 lib/tenon/tenon_content.rb
tenon-1.1.1 lib/tenon/tenon_content.rb
tenon-1.0.76 lib/tenon/tenon_content.rb
tenon-1.0.75 lib/tenon/tenon_content.rb