Sha256: 823ea87150b73b48a86efd0c4f03fb56f9813b254dd560c992e386c467e478a3

Contents?: true

Size: 1.58 KB

Versions: 4

Compression:

Stored size: 1.58 KB

Contents

module Guts
  # This concern allows for a model to be "navigatable"
  # in the admin panel for use with developing menu links
  module NavigatableConcern
    extend ActiveSupport::Concern

    # Renders a string based on options from the model
    # @note This method uses instance_eval to get the instance variables..
    #   :title to self.title, :type.title to self.type.title
    # @return [String] compiled string based on format and variables
    # @see Guts::NavigatableConcern::ClassMethods#navigatable
    def navigatable_format
      formatted = self.class.navigatable_opts[:format]
      self.class.navigatable_opts[:variables].each do |var|
        formatted = formatted.gsub /\:#{var.to_s}/, self.instance_eval("self.#{var.to_s}")
      end
      
      formatted
    end
    
    # Class methods for the concern
    # @attr [Hash] navigatable_opts the options
    module ClassMethods
      # @return [Hash] the configuration for the navigatable object
      attr_accessor :navigatable_opts

      # Allows a class to be navigatable
      # @param [Array] variables the variables to access from the model
      # @param [Hash] opts the options for the concern
      # @option opts [String] :format the text format to use with variables
      # @example Example for Content model
      #   # This will convert the format into something like: "[Page] Test"
      #   navigatable :"type.title", :title, format: "[:type.title] :title"
      def navigatable(*variables, **opts)
        @navigatable_opts = {
          variables: variables,
          format: opts[:format]
        }
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
guts-1.0.8 app/concerns/guts/navigatable_concern.rb
guts-1.0.7 app/concerns/guts/navigatable_concern.rb
guts-1.0.5 app/concerns/guts/navigatable_concern.rb
guts-1.0.3 app/concerns/guts/navigatable_concern.rb