Sha256: dfb33f0a1813731bbfb336b388bbd7fd80647b9f60db961c57d2b86b73ed94fe

Contents?: true

Size: 1.75 KB

Versions: 3

Compression:

Stored size: 1.75 KB

Contents

module Faalis
  module Generators
    module Concerns

      # This **concern** adds the methods needed to support nested resource
      # in any dashboad generators. This module adds `parents` key to jsonfile
      # which is an array of resource parents in right order.
      # For example if we have a nested resource like:
      #
      # ```ruby
      # resources :blogs do
      #   resources :categories do
      #     resources :posts
      #   end
      # end
      # ```
      #
      # And we want to create an dashboard scaffold for `post` resource we have
      # to add `parent` key to our json file like this:
      #
      # ```json
      # ...
      # "parents": [
      #     "blog",
      #     "category"
      # ]
      # ...
      # ```
      # Please pay attention to singular form of name of parents in json defination
      module Parent

        def self.included(base)
          # Specify the parent resource if there was any
          #base.class_option :parents, :type => :string, :default => "", :desc => "Specify the parent resource if there was any"
        end

        private

        # check for parent
        def parent?
          if resource_data.include? "parents"
            unless resource_data["parents"].nil?
              return true
            end
          end
          false
        end

        # Remove the starting slash from the given parent path
        def trim_parent_path(path)
          path.gsub(/^\//, "")
        end

        # Return an array of resource parents
        def parents
          if parent?
            _parents = resource_data["parents"]
            _parents.collect do |p|
              trim_parent_path(p)
            end
          else
            []
          end
        end

      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
faalis-0.24.2 lib/faalis/generators/concerns/parent.rb
faalis-0.24.0 lib/faalis/generators/concerns/parent.rb
faalis-0.23.0 lib/faalis/generators/concerns/parent.rb