Skip to content

Commit

Permalink
Merge pull request #122 from Zero-Config-Rails/update-vcr-generator
Browse files Browse the repository at this point in the history
Update vcr and devise generator with minor improvements
  • Loading branch information
abhaynikam authored Sep 26, 2024
2 parents ac303c2 + 526585e commit 68133b1
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ def add_capybara_configurations
def setup
Capybara.server_host = "0.0.0.0" # bind to all interfaces
Capybara.server_port = 3000
ip = Socket.ip_address_list.detect(&:ipv4_private?).ip_address
Capybara.app_host = "http://\#{ip}:\#{Capybara.server_port}" if ENV["SELENIUM_REMOTE_URL"].present?
if ENV["SELENIUM_REMOTE_URL"].present?
ip = Socket.ip_address_list.detect(&:ipv4_private?).ip_address
Capybara.app_host = "http://\#{ip}:\#{Capybara.server_port}"
end
super
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ test:
adapter: postgresql
encoding: unicode
host: postgres
username: test
password: test
username: postgres
<%- elsif @database == "mysql" -%>
adapter: mysql2
encoding: utf8mb4
Expand All @@ -18,4 +17,4 @@ test:
<%- unless @database == "sqlite3" -%>
database: ci_db
<%- end -%>
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
pool: <%%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
48 changes: 32 additions & 16 deletions lib/generators/boring/devise/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,28 @@ class InstallGenerator < Rails::Generators::Base

DEFAULT_DEVISE_MODEL_NAME = "User"

class_option :model_name, type: :string, aliases: "-m",
desc: "Tell us the user model name which will be used for authentication. Defaults to #{DEFAULT_DEVISE_MODEL_NAME}"
class_option :skip_devise_view, type: :boolean, aliases: "-sv",
class_option :model_name,
type: :string,
aliases: "-m",
desc:
"Tell us the user model name which will be used for authentication. Defaults to #{DEFAULT_DEVISE_MODEL_NAME}"
class_option :skip_devise_view,
type: :boolean,
aliases: "-sv",
desc: "Skip generating devise views"
class_option :skip_devise_model, type: :boolean, aliases: "-sm",
class_option :skip_devise_model,
type: :boolean,
aliases: "-sm",
desc: "Skip generating devise model"
class_option :run_db_migrate, type: :boolean, aliases: "-rdm",
class_option :run_db_migrate,
type: :boolean,
aliases: "-rdm",
desc: "Run migrations after generating user table",
default: false

def add_devise_gem
say "Adding devise gem", :green
Bundler.with_unbundled_env do
run "bundle add devise"
end
Bundler.with_unbundled_env { run "bundle add devise" }
end

def generating_devise_defaults
Expand All @@ -33,10 +40,12 @@ def generating_devise_defaults

def add_devise_action_mailer_development_config
say "Adding devise Action Mailer development configuration", :green
insert_into_file "config/environments/development.rb", <<~RUBY, after: /Rails.application.configure do/
insert_into_file "config/environments/development.rb",
<<~RUBY,
\n
\tconfig.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
RUBY
after: /Rails.application.configure do/
end

def add_devise_user_model
Expand All @@ -49,18 +58,27 @@ def add_devise_user_model
run "DISABLE_SPRING=1 bundle exec rails generate devise #{model_name}"
end

# comment out user fixture because it fails the test due to email not being unique
# make the email unique
if File.exist?("test/fixtures/users.yml")
comment_lines "test/fixtures/users.yml", /one: {}/
comment_lines "test/fixtures/users.yml", /two: {}/
email_content = <<~FIXTURE
one:
email: [email protected]
FIXTURE

gsub_file "test/fixtures/users.yml",
/one: {}/,
optimize_indentation(email_content, 0)
end
end

def add_devise_authentication_filter_to_application_controller
insert_into_file "app/controllers/application_controller.rb", <<~RUBY, after: /class ApplicationController < ActionController::Base/
insert_into_file "app/controllers/application_controller.rb",
<<~RUBY,
\n
\tbefore_action :authenticate_user!
RUBY
after:
/class ApplicationController < ActionController::Base/
end

def add_devise_views
Expand All @@ -77,9 +95,7 @@ def add_devise_views
def run_db_migrate
return unless options[:run_db_migrate]

Bundler.with_unbundled_env do
rails_command "db:migrate"
end
Bundler.with_unbundled_env { rails_command "db:migrate" }
end
end
end
Expand Down
9 changes: 5 additions & 4 deletions lib/generators/boring/vcr/install/install_generator.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# frozen_string_literal: true

require 'boring_generators/generator_helper'
require "boring_generators/generator_helper"

module Boring
module Vcr
class InstallGenerator < Rails::Generators::Base
include BoringGenerators::GeneratorHelper

desc "Adds VCR to the application"
source_root File.expand_path("templates", __dir__)

Expand Down Expand Up @@ -46,7 +46,7 @@ def verify_presence_of_rails_helper

def add_vcr_gem
say "Adding VCR gems to Gemfile", :green

check_and_install_gem "vcr", group: :test
end

Expand Down Expand Up @@ -83,9 +83,10 @@ def setup_vcr_for_minitest
require "vcr"
VCR.configure do |c|
c.cassette_library_dir = "test/vcr"
c.cassette_library_dir = "test/vcr_cassettes"
c.hook_into #{format_stubbing_libraries}
c.ignore_localhost = true
c.allow_http_connections_when_no_cassette = true
end
RUBY

Expand Down
3 changes: 2 additions & 1 deletion lib/generators/boring/vcr/install/templates/rspec/vcr.rb.tt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require "vcr"

VCR.configure do |c|
c.cassette_library_dir = "spec/cassettes"
c.cassette_library_dir = "spec/vcr_cassettes"
c.hook_into <%= @stubbing_libraries %>
c.configure_rspec_metadata!
c.ignore_localhost = true
c.allow_http_connections_when_no_cassette = true
end
3 changes: 1 addition & 2 deletions test/generators/ci/gitlab_ci_install_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ def test_should_add_configurations_for_postgresql
assert_file("config/database.yml.ci") do |content|
assert_match(/adapter: postgresql/, content)
refute_match(%r{database: db/test.sqlite3}, content)
assert_match(/username: test/, content)
assert_match(/password: test/, content)
assert_match(/username: postgres/, content)
end
end
end
Expand Down

0 comments on commit 68133b1

Please sign in to comment.