Skip to content

Commit

Permalink
Inject include Clearance::User inside model body (#677)
Browse files Browse the repository at this point in the history
* Inject `include Clearance::User` inside model body

Clearance generator was inserting `include Clearance::User` between
`class User <` and `ActiveRecord::Base`, which is not correct.

Our spec was not checking if the generator was including clearance
user model correctly or not. Let's add the test to ensure the generator
is inserting the content properly. This commit fixes #676.

* Add versionize app templates for test helpers
  • Loading branch information
abunashir authored and Derek Prior committed May 12, 2016
1 parent 359e9a6 commit 233fe9d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/generators/clearance/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def create_or_inject_clearance_into_user_model
inject_into_file(
"app/models/user.rb",
" include Clearance::User\n\n",
after: "class User < ",
after: "class User < #{models_inherit_from}\n",
)
else
@inherit_from = models_inherit_from
Expand Down
5 changes: 5 additions & 0 deletions spec/app_templates/app/models/rails5/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class User < ApplicationRecord
def previously_existed?
true
end
end
9 changes: 7 additions & 2 deletions spec/generators/clearance/install/install_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@

expect(user_class).to exist
expect(user_class).to have_correct_syntax
expect(user_class).to contain_models_inherit_from
expect(user_class).to contain("include Clearance::User")
expect(user_class).to contain proper_model_superclass
end
end

Expand All @@ -52,6 +52,7 @@

expect(user_class).to exist
expect(user_class).to have_correct_syntax
expect(user_class).to contain_models_inherit_from
expect(user_class).to contain("include Clearance::User")
expect(user_class).to have_method("previously_existed?")
end
Expand Down Expand Up @@ -129,7 +130,11 @@ def table_does_not_exist(name)
end
end

def proper_model_superclass
def contain_models_inherit_from
contain "< #{models_inherit_from}\n"
end

def models_inherit_from
if Rails.version >= "5.0.0"
"ApplicationRecord"
else
Expand Down
10 changes: 9 additions & 1 deletion spec/support/generator_spec_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def provide_existing_application_controller
end

def provide_existing_user_class
copy_to_generator_root("app/models", "user.rb")
copy_to_generator_root("app/models", versionize_template("user.rb"))
allow(File).to receive(:exist?).and_call_original
allow(File).to receive(:exist?).with("app/models/user.rb").and_return(true)
end
Expand All @@ -32,6 +32,14 @@ def copy_to_generator_root(destination, template)
FileUtils.mkdir_p(destination)
FileUtils.cp(template_file, destination)
end

def versionize_template(template_file)
if Rails.version >= "5.0.0"
template_file = ["rails5", template_file].join("/")
end

template_file
end
end

RSpec.configure do |config|
Expand Down

0 comments on commit 233fe9d

Please sign in to comment.