Sha256: e89d9629e0f73c41e503e9c1906c4cbe43001ad05466e062ea36f3f751fd6f95

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

module Panel
  class ResourceGenerator < Rails::Generators::NamedBase
    
    source_root File.expand_path('../templates', __FILE__)

    def add_route
      insert_into_file 'config/routes.rb', after: /namespace :panel(.*)/ do |match|
        "#{match}\n    resources :#{table_name}"
      end
    end    
    
    def add_scope
      insert_into_file "app/models/#{singular_name}.rb", after: /class #{singular_name.camelize}(.*)/ do
        "\n\n  scope :search, ->(term) { where('', term: \"%\#{term}%\") }"
      end
    end

    def add_templates
      @param_singular = ":#{singular_name}"
      @param_plural = ":#{plural_name}"
      @instance_singular = "@#{singular_name}"
      @instance_plural = "@#{plural_name}"
      @index_path = "panel_#{index_helper}_path"
      @params_method = "#{singular_name}_params"
      template 'resource/resource_controller.rb', "app/controllers/panel/#{table_name}_controller.rb"
      template 'resource/_form.html.erb', "app/views/panel/#{table_name}/_form.html.erb"
      template 'resource/new.html.erb', "app/views/panel/#{table_name}/new.html.erb"
      template 'resource/edit.html.erb', "app/views/panel/#{table_name}/edit.html.erb"
      template 'resource/index.html.erb', "app/views/panel/#{table_name}/index.html.erb"
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
panel-0.1.5 lib/generators/panel/resource_generator.rb