Sha256: a149f5d431654545e12ee512f6910634d780ccc63a671df22086e8783eb62b44

Contents?: true

Size: 990 Bytes

Versions: 3

Compression:

Stored size: 990 Bytes

Contents

module Viewable
  module Field
    module Url
      extend ActiveSupport::Concern

      included do
        before_update :normalize_url
        after_commit :reload_routes

        scope :with_url, -> { where('url LIKE :url', url: '/%') }
      end

      class_methods do
        def names
          "Naming::#{name}".constantize.names
        end

        def urls
          localized.with_url.pluck(:url)
        end

        def all_urls
          paths = names.map do |name|
            Rails.application.routes.url_helpers.try "#{name}_path"
          end.compact
          (paths + urls).sort.uniq
        end
      end

      private

      def normalize_url
        if url.present?
          segments = url.split('/').reject(&:blank?)
          segments.map{ |segment| segment.to_slug.normalize.to_s }
          self.url = segments.join('/').prepend('/')
        end
      end

      def reload_routes
        Rails.application.routes_reloader.reload!
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rails_admin_cms-0.0.6 app/models/viewable/field/url.rb
rails_admin_cms-0.0.5 app/models/viewable/field/url.rb
rails_admin_cms-0.0.4 app/models/viewable/field/url.rb