Sha256: cf90a99a88c3129bf2f5482ba1968fdb2533c05388a2e32afaaffb9923aec2c8

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

require 'rails/generators/base'

module Grape
  module Generators
    module Transformations
      class EntityGenerator < Rails::Generators::Base

        desc <<-DESC.strip_heredoc
          Create inherited Grape::Entity entity in your app/api/.../entities folder. this 
          created entity will have related with grape-transformations naming conventions

          For example:

            rails generate entity user

          This will create a entity class at app/api/.../entities/users/default.rb like this:

            module TestApp
              module Entities
                module Users
                  class Default < Grape::Entity
                    
                  end
                end
              end
            end 
        DESC

        source_root File.expand_path('../../templates', __FILE__)

        argument :entity_name, type: :string, required: true, desc: 'name of entity'
        argument :fields, :type => :array, required: false, desc: 'field set that you want to expose'
        
        def generate_layout
          @fields ||= []
          template "entity.rb", "app/api/#{app_name}/entities/#{underscored_entity_name.pluralize}/default.rb"
        end

        private

        # Returns the app name
        # @return [String]
        def app_name
          Rails.application.config.session_options[:key].sub(/^_/,'').sub(/_session/,'')
        end

        def underscored_entity_name
          entity_name.underscore
        end

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grape-transformations-0.0.1 lib/grape/generators/transformations/entity_generator.rb