Skip to content

Commit

Permalink
Let users undo the administrate:views generator
Browse files Browse the repository at this point in the history
Fixes #310

Problem:

Rails provides a way to undo the effects of generators,
by running the command `rails destroy GENERATOR_NAME`.

With the way that we had set up our hierarchy of generators,
Rails did not know how to undo the effects of sub-generators.

For example, with the `administrate:views` generator,
we were calling `Rails::Generators.invoke("administrate:views:index")`.

Whether the generator was run with the `generate` or `destroy` command
did not make a difference -
the index generator would always be invoked
as if it were run with `generate`.

Solution:

Rails generators use the `@behavior` instance variable to keep track of
how the generator was run.
This variable can be either `:invoke` or `:revoke`.

Pass this variable as an option to the sub-generators
to ensure they have the same behavior as the parent.

Minor changes:

Extract `Administrate::GeneratorHelpers` to store common
generator-related methods.
  • Loading branch information
c-lliope committed Jan 22, 2016
1 parent 2176d6a commit 1146204
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 17 deletions.
13 changes: 13 additions & 0 deletions lib/administrate/generator_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Administrate
module GeneratorHelpers
def call_generator(generator, *args)
Rails::Generators.invoke(generator, args, generator_options)
end

private

def generator_options
{ behavior: behavior }
end
end
end
3 changes: 3 additions & 0 deletions lib/administrate/view_generator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
require "rails/generators/base"
require "administrate/generator_helpers"

module Administrate
class ViewGenerator < Rails::Generators::Base
include Administrate::GeneratorHelpers

private

def self.template_source_path
Expand Down
6 changes: 3 additions & 3 deletions lib/generators/administrate/assets/assets_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ module Administrate
module Generators
class AssetsGenerator < Administrate::ViewGenerator
def copy_assets
Rails::Generators.invoke("administrate:assets:images")
Rails::Generators.invoke("administrate:assets:javascripts")
Rails::Generators.invoke("administrate:assets:stylesheets")
call_generator("administrate:assets:images")
call_generator("administrate:assets:javascripts")
call_generator("administrate:assets:stylesheets")
end
end
end
Expand Down
6 changes: 4 additions & 2 deletions lib/generators/administrate/install/install_generator.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
require "rails/generators/base"
require "administrate/generator_helpers"

module Administrate
module Generators
class InstallGenerator < Rails::Generators::Base
include Administrate::GeneratorHelpers
source_root File.expand_path("../templates", __FILE__)

def create_dashboard_controller
Expand All @@ -20,7 +22,7 @@ def insert_dashboard_routes

def run_dashboard_generators
singular_dashboard_resources.each do |resource|
Rails::Generators.invoke("administrate:dashboard", [resource])
call_generator("administrate:dashboard", resource)
end
end

Expand All @@ -36,7 +38,7 @@ def dashboard_resources

def manifest
unless defined?(DashboardManifest)
Rails::Generators.invoke("administrate:manifest")
call_generator("administrate:manifest")
end

DashboardManifest
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/administrate/views/layout_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def copy_template
"app/views/layouts/admin/application.html.erb",
)

Rails::Generators.invoke("administrate:views:sidebar")
call_generator("administrate:views:sidebar")
copy_resource_template("_javascript")
copy_resource_template("_flashes")
end
Expand Down
8 changes: 4 additions & 4 deletions lib/generators/administrate/views/views_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ module Administrate
module Generators
class ViewsGenerator < Administrate::ViewGenerator
def copy_templates
Rails::Generators.invoke("administrate:views:index", [resource_path])
Rails::Generators.invoke("administrate:views:show", [resource_path])
Rails::Generators.invoke("administrate:views:new", [resource_path])
Rails::Generators.invoke("administrate:views:edit", [resource_path])
call_generator("administrate:views:index", resource_path)
call_generator("administrate:views:show", resource_path)
call_generator("administrate:views:new", resource_path)
call_generator("administrate:views:edit", resource_path)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/generators/install_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ class ApplicationController < Administrate::ApplicationController
run_generator

%w[customer order product line_item].each do |resource|
expect(Rails::Generators).to have_received(:invoke).
with("administrate:dashboard", [resource])
expect(Rails::Generators).
to invoke_generator("administrate:dashboard", [resource])
end
end
end
Expand Down
19 changes: 16 additions & 3 deletions spec/generators/views_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "spec_helper"
require "rails_helper"
require "generators/administrate/views/views_generator"
require "support/generator_spec_helpers"

Expand All @@ -11,9 +11,22 @@
run_generator [resource]

%w[index show new edit].each do |generator|
expect(Rails::Generators).to have_received(:invoke).
with("administrate:views:#{generator}", [resource])
expect(Rails::Generators).
to invoke_generator("administrate:views:#{generator}", [resource])
end
end

it "revokes sub-generators if run through `rails destroy`" do
allow(Rails::Generators).to receive(:invoke)
resource = "users"

run_generator [resource], behavior: :revoke

expect(Rails::Generators).to invoke_generator(
"administrate:views:index",
[resource],
behavior: :revoke,
)
end
end
end
4 changes: 2 additions & 2 deletions spec/support/generator_spec_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def contents_for_application_template(view_name)
)
end

def invoke_generator(*args)
have_received(:invoke).with(*args)
def invoke_generator(generator, args = [], options = { behavior: :invoke })
have_received(:invoke).with(generator, args, options)
end

def each_file_in(path)
Expand Down

0 comments on commit 1146204

Please sign in to comment.