Sha256: 6ed423f4027b0e75899cf682657ed5334cfa188280aebc144009e0e8240e5cf2

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

module CmAdmin
  module ViewActionTemplate
    def copy_index_files
      template "views/index.erb", "app/views/admin/#{name}/index.html.slim"
      template "views/_table.erb", "app/views/admin/#{name}/_table.html.slim"
      copy_file "views/_filters.html.slim", "app/views/admin/#{name}/_filters.html.slim"
    end

    def copy_show_files
      template "views/show.erb", "app/views/admin/#{name}/show.html.slim"
    end

    def copy_form_files
      template "views/_form.erb", "app/views/admin/#{name}/_form.html.slim"
    end
  end

  class ViewGenerator < Rails::Generators::NamedBase
    include ViewActionTemplate
    desc <<-DESC.strip_heredoc
      Generate views for specific action of the model in admin
      Use -c to specify which column names you want to be present on page.
      Atleast one option is mandatory.
      For example:
        rails g cm_admin:view NAME page_type -c options
        rails g cm_admin:view products form -c title:string price:integer
        rails g cm_admin:view products index -c title price

      NAME is the table name
      page_type is the page you want to copy files. It can be the following
      index, show, form.

      This will create a files respectively at app/views/admin/#{name}/***.html.slim

    DESC

    source_root File.expand_path('templates', __dir__)

    argument :page_type, required: true, default: nil,
                         desc: "The scope to copy views to"
    class_option :column_names, aliases: "-c", type: :array,
                  desc: "Select specific columns in the files to be be added."

    def create_new_file
      if page_type == 'index'
        copy_index_files
      elsif page_type == 'form'
        copy_form_files
      elsif page_type == 'show'
        copy_show_files
      else
        puts "Wrong page_type provided. It can be either index or show"
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cm-admin-0.1.0 lib/generators/cm_admin/view_generator.rb