Sha256: 935fedaa8585df50bd1106c13b5f914bd2568587650386774bfd9c5164c7e8bd

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

module Enjoy::Pages::Connectable
  extend ActiveSupport::Concern

  module ClassMethods
    def enjoy_connectable_field (name = :connected_pages, opts = {})
      class_name = opts.delete(:class_name)
      class_name ||= "Enjoy::Pages::Page"
      routes_namespace = opts.delete(:routes_namespace)
      routes_namespace ||= :main_app
      autocreate_page = opts.delete(:autocreate_page)
      autocreate_page = false if autocreate_page.nil?

      has_many name, as: :enjoy_connectable, class_name: class_name
      class_eval <<-EVAL
        def routes_namespace
          :#{routes_namespace}
        end
      EVAL

      if autocreate_page
        attr_accessor :enjoy_connectable_autocreate_page
        _name_attr = Enjoy::Pages.config.localize ? "name_translations" : "name"
        class_eval <<-EVAL
          after_create do
            if [true, 1, "1", "true", "t"].include?(self.enjoy_connectable_autocreate_page)
              _p = #{class_name}.new
              _p.#{_name_attr} = self.#{_name_attr}
              _p.connectable = self
              _p.save
            end
          end
        EVAL
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
enjoy_cms_pages-0.4.1 app/models/concerns/enjoy/pages/connectable.rb