From 0bad5db6b761fa69182c5e031d2f3eb1670ceb18 Mon Sep 17 00:00:00 2001 From: Jorge-Ortiz-Mata Date: Mon, 17 Oct 2022 10:51:12 -0500 Subject: [PATCH 01/12] Adding RuboCop gem and Rpec gems. --- Gemfile.lock | 38 +++++++++++++++++++ icalialabs-firebase.gemspec | 3 ++ .../icalialabs_firebase_generator.rb | 4 +- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index c384b64..4a5c040 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -3,6 +3,8 @@ PATH specs: icalialabs-firebase (0.1.0) rails (>= 7.0.4) + rspec + rubocop GEM remote: https://rubygems.org/ @@ -72,14 +74,17 @@ GEM i18n (>= 1.6, < 2) minitest (>= 5.1) tzinfo (~> 2.0) + ast (2.4.2) builder (3.2.4) concurrent-ruby (1.1.10) crass (1.0.6) + diff-lcs (1.5.0) erubi (1.11.0) globalid (1.0.0) activesupport (>= 5.0) i18n (1.12.0) concurrent-ruby (~> 1.0) + json (2.6.2) loofah (2.19.0) crass (~> 1.0.2) nokogiri (>= 1.5.9) @@ -100,6 +105,9 @@ GEM nio4r (2.5.8) nokogiri (1.13.8-x86_64-linux) racc (~> 1.4) + parallel (1.22.1) + parser (3.1.2.1) + ast (~> 2.4.1) racc (1.6.0) rack (2.2.4) rack-test (2.0.2) @@ -130,7 +138,36 @@ GEM rake (>= 12.2) thor (~> 1.0) zeitwerk (~> 2.5) + rainbow (3.1.1) rake (13.0.6) + regexp_parser (2.6.0) + rexml (3.2.5) + rspec (3.11.0) + rspec-core (~> 3.11.0) + rspec-expectations (~> 3.11.0) + rspec-mocks (~> 3.11.0) + rspec-core (3.11.0) + rspec-support (~> 3.11.0) + rspec-expectations (3.11.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.11.0) + rspec-mocks (3.11.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.11.0) + rspec-support (3.11.1) + rubocop (1.36.0) + json (~> 2.3) + parallel (~> 1.10) + parser (>= 3.1.2.1) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.20.1, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 1.4.0, < 3.0) + rubocop-ast (1.21.0) + parser (>= 3.1.1.0) + ruby-progressbar (1.11.0) sprockets (4.1.1) concurrent-ruby (~> 1.0) rack (> 1, < 3) @@ -142,6 +179,7 @@ GEM timeout (0.3.0) tzinfo (2.0.5) concurrent-ruby (~> 1.0) + unicode-display_width (2.3.0) websocket-driver (0.7.5) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) diff --git a/icalialabs-firebase.gemspec b/icalialabs-firebase.gemspec index 2ebe810..19f0484 100644 --- a/icalialabs-firebase.gemspec +++ b/icalialabs-firebase.gemspec @@ -23,4 +23,7 @@ Gem::Specification.new do |spec| end spec.add_dependency "rails", ">= 7.0.4" + + spec.add_dependency "rubocop" + spec.add_dependency "rspec" end diff --git a/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb b/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb index cf63802..a46e49a 100644 --- a/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb +++ b/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb @@ -18,8 +18,8 @@ def create_views def current_user @current_user ||= session[:user_id] end - RUBY - end + RUBY + end # ---------- Views argument. ------------- elsif class_name == 'Views' From 54dd44da74943d39bc677cbe7c2031645944e3f6 Mon Sep 17 00:00:00 2001 From: Jorge-Ortiz-Mata Date: Mon, 17 Oct 2022 18:37:51 +0000 Subject: [PATCH 02/12] Adding gems for testing. --- Gemfile.lock | 9 +++++++++ icalialabs-firebase.gemspec | 1 + 2 files changed, 10 insertions(+) diff --git a/Gemfile.lock b/Gemfile.lock index 4a5c040..61e8259 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -4,6 +4,7 @@ PATH icalialabs-firebase (0.1.0) rails (>= 7.0.4) rspec + rspec-rails rubocop GEM @@ -154,6 +155,14 @@ GEM rspec-mocks (3.11.1) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.11.0) + rspec-rails (6.0.0) + actionpack (>= 6.1) + activesupport (>= 6.1) + railties (>= 6.1) + rspec-core (~> 3.11) + rspec-expectations (~> 3.11) + rspec-mocks (~> 3.11) + rspec-support (~> 3.11) rspec-support (3.11.1) rubocop (1.36.0) json (~> 2.3) diff --git a/icalialabs-firebase.gemspec b/icalialabs-firebase.gemspec index 19f0484..fde01a7 100644 --- a/icalialabs-firebase.gemspec +++ b/icalialabs-firebase.gemspec @@ -26,4 +26,5 @@ Gem::Specification.new do |spec| spec.add_dependency "rubocop" spec.add_dependency "rspec" + spec.add_dependency "rspec-rails" end From aa1d9e887099ce252c1cda44f84f38121bc27abf Mon Sep 17 00:00:00 2001 From: Jorge-Ortiz-Mata Date: Mon, 17 Oct 2022 18:45:39 +0000 Subject: [PATCH 03/12] Installing RSpec. --- .rspec | 1 + spec/rails_helper.rb | 64 ++++++++++++++++++++++++++++++ spec/spec_helper.rb | 94 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 159 insertions(+) create mode 100644 .rspec create mode 100644 spec/rails_helper.rb create mode 100644 spec/spec_helper.rb diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..c99d2e7 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--require spec_helper diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 0000000..7951aa6 --- /dev/null +++ b/spec/rails_helper.rb @@ -0,0 +1,64 @@ +# This file is copied to spec/ when you run 'rails generate rspec:install' +require File.expand_path('../dummy/config/environment', __FILE__) +require 'spec_helper' +ENV['RAILS_ENV'] ||= 'test' +require_relative '../config/environment' +# Prevent database truncation if the environment is production +abort("The Rails environment is running in production mode!") if Rails.env.production? +require 'rspec/rails' +# Add additional requires below this line. Rails is not loaded until this point! + +# Requires supporting ruby files with custom matchers and macros, etc, in +# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are +# run as spec files by default. This means that files in spec/support that end +# in _spec.rb will both be required and run as specs, causing the specs to be +# run twice. It is recommended that you do not name files matching this glob to +# end with _spec.rb. You can configure this pattern with the --pattern +# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. +# +# The following line is provided for convenience purposes. It has the downside +# of increasing the boot-up time by auto-requiring all files in the support +# directory. Alternatively, in the individual `*_spec.rb` files, manually +# require only the support files necessary. +# +# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f } + +# Checks for pending migrations and applies them before tests are run. +# If you are not using ActiveRecord, you can remove these lines. +begin + ActiveRecord::Migration.maintain_test_schema! +rescue ActiveRecord::PendingMigrationError => e + abort e.to_s.strip +end +RSpec.configure do |config| + # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures + config.fixture_path = "#{::Rails.root}/spec/fixtures" + + # If you're not using ActiveRecord, or you'd prefer not to run each of your + # examples within a transaction, remove the following line or assign false + # instead of true. + config.use_transactional_fixtures = true + + # You can uncomment this line to turn off ActiveRecord support entirely. + # config.use_active_record = false + + # RSpec Rails can automatically mix in different behaviours to your tests + # based on their file location, for example enabling you to call `get` and + # `post` in specs under `spec/controllers`. + # + # You can disable this behaviour by removing the line below, and instead + # explicitly tag your specs with their type, e.g.: + # + # RSpec.describe UsersController, type: :controller do + # # ... + # end + # + # The different available types are documented in the features, such as in + # https://relishapp.com/rspec/rspec-rails/docs + config.infer_spec_type_from_file_location! + + # Filter lines from Rails gems in backtraces. + config.filter_rails_from_backtrace! + # arbitrary gems may also be filtered via: + # config.filter_gems_from_backtrace("gem name") +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..a0d4080 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,94 @@ +# This file was generated by the `rails generate rspec:install` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # This allows you to limit a spec run to individual examples or groups + # you care about by tagging them with `:focus` metadata. When nothing + # is tagged with `:focus`, all examples get run. RSpec also provides + # aliases for `it`, `describe`, and `context` that include `:focus` + # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + config.filter_run_when_matching :focus + + # Allows RSpec to persist some state between runs in order to support + # the `--only-failures` and `--next-failure` CLI options. We recommend + # you configure your source control system to ignore this file. + config.example_status_persistence_file_path = "spec/examples.txt" + + # Limits the available syntax to the non-monkey patched syntax that is + # recommended. For more details, see: + # https://relishapp.com/rspec/rspec-core/docs/configuration/zero-monkey-patching-mode + config.disable_monkey_patching! + + # Many RSpec users commonly either run the entire suite or an individual + # file, and it's useful to allow more verbose output when running an + # individual spec file. + if config.files_to_run.one? + # Use the documentation formatter for detailed output, + # unless a formatter has already been configured + # (e.g. via a command-line flag). + config.default_formatter = "doc" + end + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed +=end +end From 54c2699338c24be73a16b70751a43cc328441910 Mon Sep 17 00:00:00 2001 From: Jorge-Ortiz-Mata Date: Mon, 17 Oct 2022 19:13:47 +0000 Subject: [PATCH 04/12] Implementing RuboCop for better practices. --- .../icalialabs_firebase_generator.rb | 9 ++++----- .../templates/firebase_controller.rb | 18 +++++++++++------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb b/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb index a46e49a..9fef015 100644 --- a/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb +++ b/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb @@ -1,11 +1,10 @@ class IcalialabsFirebaseGenerator < Rails::Generators::NamedBase - source_root File.expand_path("../../../", __dir__) + source_root File.expand_path('../../../', __dir__) def create_views # ---------- Install argument. ------------- if class_name == 'Install' - template 'lib/generators/icalialabs_firebase/templates/firebase_controller.rb', - File.join('app/controllers', class_path, "firebase_controller.rb") + template 'lib/generators/icalialabs_firebase/templates/firebase_controller.rb', File.join('app/controllers', class_path, "firebase_controller.rb") route "get '/login', to: 'firebase#login'" route "get '/signup', to: 'firebase#signup'" route "post '/create_user', to: 'firebase#create_user'" @@ -33,8 +32,8 @@ def current_user <%= button_to 'Log in', login_path, method: :get %> <%= button_to 'Sign up', signup_path, method: :get %> <% end %> - RUBY - end + RUBY + end end end end diff --git a/lib/generators/icalialabs_firebase/templates/firebase_controller.rb b/lib/generators/icalialabs_firebase/templates/firebase_controller.rb index f3fc0c0..a477d53 100644 --- a/lib/generators/icalialabs_firebase/templates/firebase_controller.rb +++ b/lib/generators/icalialabs_firebase/templates/firebase_controller.rb @@ -10,32 +10,36 @@ def login; end def signup; end def create_user - uri = URI("https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=HERE_GOES_MY_API_KEY") + uri = URI('https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=HERE_GOES_MY_API_KEY') response = Net::HTTP.post_form(uri, "email": @email, "password": @password) data = JSON.parse(response.body) - session[:user_id] = data["localId"] + session[:user_id] = data['localId'] session[:data] = data - redirect_to root_path, notice: "Sign Up successfully." if response.is_a?(Net::HTTPSuccess) + if response.is_a?(Net::HTTPSuccess) + redirect_to root_path, notice: 'Sign Up successfully.' + else + redirect_to signup_path, notice: 'Something happend, try again.' + end end def create_session - uri = URI("https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=HERE_GOES_MY_API_KEY") + uri = URI('https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=HERE_GOES_MY_API_KEY') response = Net::HTTP.post_form(uri, "email": @email, "password": @password) data = JSON.parse(response.body) if response.is_a?(Net::HTTPSuccess) - session[:user_id] = data["localId"] + session[:user_id] = data['localId'] session[:data] = data - redirect_to root_path, notice: "Log in successfully." + redirect_to root_path, notice: 'Log in successfully.' end end def logout session.clear - redirect_to root_path, notice: "Successfully log out" + redirect_to root_path, notice: 'Successfully log out' end private From d0ea1d0ab65df891afc91b63e22aa7a6c1d220c3 Mon Sep 17 00:00:00 2001 From: Jorge-Ortiz-Mata Date: Mon, 17 Oct 2022 20:49:48 +0000 Subject: [PATCH 05/12] Refactoring code. --- .../icalialabs_firebase_generator.rb | 48 ++++++++++++------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb b/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb index 9fef015..bf73b39 100644 --- a/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb +++ b/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb @@ -2,40 +2,52 @@ class IcalialabsFirebaseGenerator < Rails::Generators::NamedBase source_root File.expand_path('../../../', __dir__) def create_views - # ---------- Install argument. ------------- - if class_name == 'Install' - template 'lib/generators/icalialabs_firebase/templates/firebase_controller.rb', File.join('app/controllers', class_path, "firebase_controller.rb") - route "get '/login', to: 'firebase#login'" - route "get '/signup', to: 'firebase#signup'" - route "post '/create_user', to: 'firebase#create_user'" - route "post '/create_session', to: 'firebase#create_session'" - route "delete '/logout', to: 'firebase#logout', as: 'logout'" - inject_into_file 'app/controllers/application_controller.rb', - after: "class ApplicationController < ActionController::Base\n" do <<-'RUBY' + case class_name + when 'Install' + template 'lib/generators/icalialabs_firebase/templates/firebase_controller.rb', File.join('app/controllers', class_path, "firebase_controller.rb") + firebase_routes + inject_into_file 'app/controllers/application_controller.rb', after: "class ApplicationController < ActionController::Base\n" do <<- RUBY helper_method :current_user def current_user @current_user ||= session[:user_id] end - RUBY - end - - # ---------- Views argument. ------------- - elsif class_name == 'Views' - directory 'app/views/firebase/', 'app/views/firebase/' - inject_into_file 'app/views/layouts/application.html.erb', + RUBY + end + when 'Views' + directory 'app/views/firebase/', 'app/views/firebase/' + inject_into_file 'app/views/layouts/application.html.erb', after: "\n" do <<-"RUBY" <%= notice if notice %> <% if current_user.present? %> - <%= button_to "Log Out", logout_path, to: "pages#logout", method: :delete %> + <%= button_to 'Log Out', logout_path, to: 'pages#logout', method: :delete %> <% else %> <%= button_to 'Log in', login_path, method: :get %> <%= button_to 'Sign up', signup_path, method: :get %> <% end %> RUBY end + else + message_command end end + + private + + def firebase_routes + route "get '/login', to: 'firebase#login'" + route "get '/signup', to: 'firebase#signup'" + route "post '/create_user', to: 'firebase#create_user'" + route "post '/create_session', to: 'firebase#create_session'" + route "delete '/logout', to: 'firebase#logout', as: 'logout'" + end + + def message_command + puts "Sorry, that command is not recognized." + puts "Try one of the commands below:" + puts " - rails g icalialabs_firebase install" + puts " - rails g icalialabs_firebase views" + end end From c019608ae1e2da0a01de067be71a231bfeff3a55 Mon Sep 17 00:00:00 2001 From: Jorge-Ortiz-Mata Date: Mon, 17 Oct 2022 21:02:35 +0000 Subject: [PATCH 06/12] Separating each part by using methods. --- .../icalialabs_firebase_generator.rb | 57 +++++++++++-------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb b/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb index bf73b39..37f069f 100644 --- a/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb +++ b/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb @@ -6,27 +6,10 @@ def create_views when 'Install' template 'lib/generators/icalialabs_firebase/templates/firebase_controller.rb', File.join('app/controllers', class_path, "firebase_controller.rb") firebase_routes - inject_into_file 'app/controllers/application_controller.rb', after: "class ApplicationController < ActionController::Base\n" do <<- RUBY - helper_method :current_user - - def current_user - @current_user ||= session[:user_id] - end - RUBY - end + inject_code_with_install when 'Views' directory 'app/views/firebase/', 'app/views/firebase/' - inject_into_file 'app/views/layouts/application.html.erb', - after: "\n" do <<-"RUBY" - <%= notice if notice %> - <% if current_user.present? %> - <%= button_to 'Log Out', logout_path, to: 'pages#logout', method: :delete %> - <% else %> - <%= button_to 'Log in', login_path, method: :get %> - <%= button_to 'Sign up', signup_path, method: :get %> - <% end %> - RUBY - end + inject_code_with_views else message_command end @@ -34,6 +17,13 @@ def current_user private + def message_command + puts "Sorry, that command is not recognized." + puts "Try one of the commands below:" + puts " - rails g icalialabs_firebase install" + puts " - rails g icalialabs_firebase views" + end + def firebase_routes route "get '/login', to: 'firebase#login'" route "get '/signup', to: 'firebase#signup'" @@ -42,11 +32,30 @@ def firebase_routes route "delete '/logout', to: 'firebase#logout', as: 'logout'" end - def message_command - puts "Sorry, that command is not recognized." - puts "Try one of the commands below:" - puts " - rails g icalialabs_firebase install" - puts " - rails g icalialabs_firebase views" + def inject_code_with_install + inject_into_file 'app/controllers/application_controller.rb', + after: "class ApplicationController < ActionController::Base\n" do <<-'RUBY' + helper_method :current_user + + def current_user + @current_user ||= session[:user_id] + end + RUBY + end + end + + def inject_code_with_views + inject_into_file 'app/views/layouts/application.html.erb', + after: "\n" do <<-'RUBY' + <%= notice if notice %> + <% if current_user.present? %> + <%= button_to 'Log Out', logout_path, to: 'pages#logout', method: :delete %> + <% else %> + <%= button_to 'Log in', login_path, method: :get %> + <%= button_to 'Sign up', signup_path, method: :get %> + <% end %> + RUBY + end end end From 096b0fa7242da6014a185f6cd410a496e51e8123 Mon Sep 17 00:00:00 2001 From: Jorge-Ortiz-Mata Date: Mon, 17 Oct 2022 21:13:23 +0000 Subject: [PATCH 07/12] Making changes according to Rubocop. --- .../icalialabs_firebase_generator.rb | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb b/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb index 37f069f..7e06383 100644 --- a/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb +++ b/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb @@ -4,7 +4,8 @@ class IcalialabsFirebaseGenerator < Rails::Generators::NamedBase def create_views case class_name when 'Install' - template 'lib/generators/icalialabs_firebase/templates/firebase_controller.rb', File.join('app/controllers', class_path, "firebase_controller.rb") + template 'lib/generators/icalialabs_firebase/templates/firebase_controller.rb', + File.join('app/controllers', class_path, 'firebase_controller.rb') firebase_routes inject_code_with_install when 'Views' @@ -18,10 +19,10 @@ def create_views private def message_command - puts "Sorry, that command is not recognized." - puts "Try one of the commands below:" - puts " - rails g icalialabs_firebase install" - puts " - rails g icalialabs_firebase views" + puts 'Sorry, that command is not recognized.' + puts 'Try one of the commands below:' + puts ' - rails g icalialabs_firebase install' + puts ' - rails g icalialabs_firebase views' end def firebase_routes @@ -34,7 +35,7 @@ def firebase_routes def inject_code_with_install inject_into_file 'app/controllers/application_controller.rb', - after: "class ApplicationController < ActionController::Base\n" do <<-'RUBY' + after: 'class ApplicationController < ActionController::Base\n' do <<-'RUBY' helper_method :current_user def current_user @@ -45,8 +46,7 @@ def current_user end def inject_code_with_views - inject_into_file 'app/views/layouts/application.html.erb', - after: "\n" do <<-'RUBY' + inject_into_file 'app/views/layouts/application.html.erb', after: '\n' do <<-'RUBY' <%= notice if notice %> <% if current_user.present? %> <%= button_to 'Log Out', logout_path, to: 'pages#logout', method: :delete %> @@ -54,8 +54,8 @@ def inject_code_with_views <%= button_to 'Log in', login_path, method: :get %> <%= button_to 'Sign up', signup_path, method: :get %> <% end %> - RUBY - end + RUBY + end end end From 2843201f9142a8193f0569e52b4681c1e9afece3 Mon Sep 17 00:00:00 2001 From: Jorge-Ortiz-Mata Date: Tue, 18 Oct 2022 14:54:10 +0000 Subject: [PATCH 08/12] Adding method to create files using templates, and adding tests --- .../icalialabs_firebase_generator.rb | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb b/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb index 7e06383..fcae29d 100644 --- a/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb +++ b/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb @@ -4,8 +4,7 @@ class IcalialabsFirebaseGenerator < Rails::Generators::NamedBase def create_views case class_name when 'Install' - template 'lib/generators/icalialabs_firebase/templates/firebase_controller.rb', - File.join('app/controllers', class_path, 'firebase_controller.rb') + copy_templates_with_install firebase_routes inject_code_with_install when 'Views' @@ -25,6 +24,11 @@ def message_command puts ' - rails g icalialabs_firebase views' end + def copy_templates_with_install + template 'lib/generators/icalialabs_firebase/templates/firebase_controller.rb', + File.join('app/controllers', class_path, 'firebase_controller.rb') + end + def firebase_routes route "get '/login', to: 'firebase#login'" route "get '/signup', to: 'firebase#signup'" @@ -35,7 +39,7 @@ def firebase_routes def inject_code_with_install inject_into_file 'app/controllers/application_controller.rb', - after: 'class ApplicationController < ActionController::Base\n' do <<-'RUBY' + after: "class ApplicationController < ActionController::Base\n" do <<-'RUBY' helper_method :current_user def current_user @@ -46,13 +50,13 @@ def current_user end def inject_code_with_views - inject_into_file 'app/views/layouts/application.html.erb', after: '\n' do <<-'RUBY' + inject_into_file 'app/views/layouts/application.html.erb', after: "\n" do <<-'RUBY' <%= notice if notice %> <% if current_user.present? %> - <%= button_to 'Log Out', logout_path, to: 'pages#logout', method: :delete %> + <%= button_to 'Log Out', logout_path, to: 'pages#logout', method: :delete, id: 'logout_button' %> <% else %> - <%= button_to 'Log in', login_path, method: :get %> - <%= button_to 'Sign up', signup_path, method: :get %> + <%= button_to 'Log in', login_path, method: :get, id: 'login_button' %> + <%= button_to 'Sign up', signup_path, method: :get, id: 'signup_button' %> <% end %> RUBY end From 9effb9486fc30e42ba8f683498e663cfcab3b5af Mon Sep 17 00:00:00 2001 From: Jorge-Ortiz-Mata Date: Thu, 20 Oct 2022 10:27:38 -0500 Subject: [PATCH 09/12] Removing rails gem and adding as: to routes. --- Gemfile.lock | 81 ------------------- icalialabs-firebase.gemspec | 2 +- .../icalialabs_firebase_generator.rb | 4 +- 3 files changed, 3 insertions(+), 84 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 61e8259..6dda87c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,7 +2,6 @@ PATH remote: . specs: icalialabs-firebase (0.1.0) - rails (>= 7.0.4) rspec rspec-rails rubocop @@ -10,31 +9,6 @@ PATH GEM remote: https://rubygems.org/ specs: - actioncable (7.0.4) - actionpack (= 7.0.4) - activesupport (= 7.0.4) - nio4r (~> 2.0) - websocket-driver (>= 0.6.1) - actionmailbox (7.0.4) - actionpack (= 7.0.4) - activejob (= 7.0.4) - activerecord (= 7.0.4) - activestorage (= 7.0.4) - activesupport (= 7.0.4) - mail (>= 2.7.1) - net-imap - net-pop - net-smtp - actionmailer (7.0.4) - actionpack (= 7.0.4) - actionview (= 7.0.4) - activejob (= 7.0.4) - activesupport (= 7.0.4) - mail (~> 2.5, >= 2.5.4) - net-imap - net-pop - net-smtp - rails-dom-testing (~> 2.0) actionpack (7.0.4) actionview (= 7.0.4) activesupport (= 7.0.4) @@ -42,34 +16,12 @@ GEM rack-test (>= 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.2.0) - actiontext (7.0.4) - actionpack (= 7.0.4) - activerecord (= 7.0.4) - activestorage (= 7.0.4) - activesupport (= 7.0.4) - globalid (>= 0.6.0) - nokogiri (>= 1.8.5) actionview (7.0.4) activesupport (= 7.0.4) builder (~> 3.1) erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.1, >= 1.2.0) - activejob (7.0.4) - activesupport (= 7.0.4) - globalid (>= 0.3.6) - activemodel (7.0.4) - activesupport (= 7.0.4) - activerecord (7.0.4) - activemodel (= 7.0.4) - activesupport (= 7.0.4) - activestorage (7.0.4) - actionpack (= 7.0.4) - activejob (= 7.0.4) - activerecord (= 7.0.4) - activesupport (= 7.0.4) - marcel (~> 1.0) - mini_mime (>= 1.1.0) activesupport (7.0.4) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 1.6, < 2) @@ -81,29 +33,14 @@ GEM crass (1.0.6) diff-lcs (1.5.0) erubi (1.11.0) - globalid (1.0.0) - activesupport (>= 5.0) i18n (1.12.0) concurrent-ruby (~> 1.0) json (2.6.2) loofah (2.19.0) crass (~> 1.0.2) nokogiri (>= 1.5.9) - mail (2.7.1) - mini_mime (>= 0.1.1) - marcel (1.0.2) method_source (1.0.0) - mini_mime (1.1.2) minitest (5.16.3) - net-imap (0.3.1) - net-protocol - net-pop (0.1.2) - net-protocol - net-protocol (0.1.3) - timeout - net-smtp (0.3.2) - net-protocol - nio4r (2.5.8) nokogiri (1.13.8-x86_64-linux) racc (~> 1.4) parallel (1.22.1) @@ -113,20 +50,6 @@ GEM rack (2.2.4) rack-test (2.0.2) rack (>= 1.3) - rails (7.0.4) - actioncable (= 7.0.4) - actionmailbox (= 7.0.4) - actionmailer (= 7.0.4) - actionpack (= 7.0.4) - actiontext (= 7.0.4) - actionview (= 7.0.4) - activejob (= 7.0.4) - activemodel (= 7.0.4) - activerecord (= 7.0.4) - activestorage (= 7.0.4) - activesupport (= 7.0.4) - bundler (>= 1.15.0) - railties (= 7.0.4) rails-dom-testing (2.0.3) activesupport (>= 4.2.0) nokogiri (>= 1.6) @@ -185,13 +108,9 @@ GEM activesupport (>= 5.2) sprockets (>= 3.0.0) thor (1.2.1) - timeout (0.3.0) tzinfo (2.0.5) concurrent-ruby (~> 1.0) unicode-display_width (2.3.0) - websocket-driver (0.7.5) - websocket-extensions (>= 0.1.0) - websocket-extensions (0.1.5) zeitwerk (2.6.1) PLATFORMS diff --git a/icalialabs-firebase.gemspec b/icalialabs-firebase.gemspec index fde01a7..222adb1 100644 --- a/icalialabs-firebase.gemspec +++ b/icalialabs-firebase.gemspec @@ -22,7 +22,7 @@ Gem::Specification.new do |spec| Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"] end - spec.add_dependency "rails", ">= 7.0.4" + # spec.add_dependency "rails", ">= 7.0.3" spec.add_dependency "rubocop" spec.add_dependency "rspec" diff --git a/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb b/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb index fcae29d..5b2d50d 100644 --- a/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb +++ b/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb @@ -30,8 +30,8 @@ def copy_templates_with_install end def firebase_routes - route "get '/login', to: 'firebase#login'" - route "get '/signup', to: 'firebase#signup'" + route "get '/login', to: 'firebase#login', as: 'login'" + route "get '/signup', to: 'firebase#signup', as: 'signup'" route "post '/create_user', to: 'firebase#create_user'" route "post '/create_session', to: 'firebase#create_session'" route "delete '/logout', to: 'firebase#logout', as: 'logout'" From a59a732965d41f6512d80fe0b01b1f80ddf2a081 Mon Sep 17 00:00:00 2001 From: Jorge-Ortiz-Mata Date: Thu, 20 Oct 2022 10:58:41 -0500 Subject: [PATCH 10/12] Adding firebase_spec.rb file. --- .../templates/firebase_spec.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 lib/generators/icalialabs_firebase/templates/firebase_spec.rb diff --git a/lib/generators/icalialabs_firebase/templates/firebase_spec.rb b/lib/generators/icalialabs_firebase/templates/firebase_spec.rb new file mode 100644 index 0000000..7a8bf67 --- /dev/null +++ b/lib/generators/icalialabs_firebase/templates/firebase_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require 'system_helper' + +RSpec.describe 'Firebase', type: :system do + scenario "User can see Sign Up page" do + visit signup_path + expect(page).to have_content 'Sign up' + end + + scenario "User can see Login page" do + visit login_path + expect(page).to have_content 'Log in' + end +end From 7c0175fa53a9f67609d64d14f5a7a4b8102d3ad6 Mon Sep 17 00:00:00 2001 From: Jorge-Ortiz-Mata Date: Thu, 20 Oct 2022 11:02:27 -0500 Subject: [PATCH 11/12] Adding firebase_spec template. --- .../icalialabs_firebase/icalialabs_firebase_generator.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb b/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb index 5b2d50d..6ad2992 100644 --- a/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb +++ b/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb @@ -27,6 +27,8 @@ def message_command def copy_templates_with_install template 'lib/generators/icalialabs_firebase/templates/firebase_controller.rb', File.join('app/controllers', class_path, 'firebase_controller.rb') + template 'lib/generators/icalialabs_firebase/templates/firebase_spec.rb', + File.join('app/spec/system', class_path, 'firebase_spec.rb') end def firebase_routes From 2534c8e705f7e4064c7101c2ab65968058216a38 Mon Sep 17 00:00:00 2001 From: Jorge-Ortiz-Mata Date: Thu, 20 Oct 2022 11:27:52 -0500 Subject: [PATCH 12/12] Refactoring code using readme files. --- icalialabs-firebase.gemspec | 4 +-- .../icalialabs_firebase_generator.rb | 28 ++++++------------- .../templates/error_readme.md | 11 ++++++++ .../templates/views_readme.md | 20 +++++++++++++ 4 files changed, 41 insertions(+), 22 deletions(-) create mode 100644 lib/generators/icalialabs_firebase/templates/error_readme.md create mode 100644 lib/generators/icalialabs_firebase/templates/views_readme.md diff --git a/icalialabs-firebase.gemspec b/icalialabs-firebase.gemspec index 222adb1..3c0460e 100644 --- a/icalialabs-firebase.gemspec +++ b/icalialabs-firebase.gemspec @@ -22,9 +22,9 @@ Gem::Specification.new do |spec| Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"] end - # spec.add_dependency "rails", ">= 7.0.3" - spec.add_dependency "rubocop" spec.add_dependency "rspec" spec.add_dependency "rspec-rails" + spec.add_dependency "capybara" + spec.add_dependency "cuprite" end diff --git a/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb b/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb index 6ad2992..462bcf1 100644 --- a/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb +++ b/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb @@ -9,21 +9,14 @@ def create_views inject_code_with_install when 'Views' directory 'app/views/firebase/', 'app/views/firebase/' - inject_code_with_views + views_readme else - message_command + error_readme end end private - def message_command - puts 'Sorry, that command is not recognized.' - puts 'Try one of the commands below:' - puts ' - rails g icalialabs_firebase install' - puts ' - rails g icalialabs_firebase views' - end - def copy_templates_with_install template 'lib/generators/icalialabs_firebase/templates/firebase_controller.rb', File.join('app/controllers', class_path, 'firebase_controller.rb') @@ -51,17 +44,12 @@ def current_user end end - def inject_code_with_views - inject_into_file 'app/views/layouts/application.html.erb', after: "\n" do <<-'RUBY' - <%= notice if notice %> - <% if current_user.present? %> - <%= button_to 'Log Out', logout_path, to: 'pages#logout', method: :delete, id: 'logout_button' %> - <% else %> - <%= button_to 'Log in', login_path, method: :get, id: 'login_button' %> - <%= button_to 'Sign up', signup_path, method: :get, id: 'signup_button' %> - <% end %> - RUBY - end + def views_readme + readme "lib/generators/icalialabs_firebase/templates/views_readme.md" + end + + def error_readme + readme "lib/generators/icalialabs_firebase/templates/error_readme.md" end end diff --git a/lib/generators/icalialabs_firebase/templates/error_readme.md b/lib/generators/icalialabs_firebase/templates/error_readme.md new file mode 100644 index 0000000..dafa429 --- /dev/null +++ b/lib/generators/icalialabs_firebase/templates/error_readme.md @@ -0,0 +1,11 @@ + +====================================== + +Sorry, that command is not recognized. +Try one of the commands below: + + - rails g icalialabs_firebase install + - rails g icalialabs_firebase views + +====================================== + diff --git a/lib/generators/icalialabs_firebase/templates/views_readme.md b/lib/generators/icalialabs_firebase/templates/views_readme.md new file mode 100644 index 0000000..1572ef0 --- /dev/null +++ b/lib/generators/icalialabs_firebase/templates/views_readme.md @@ -0,0 +1,20 @@ + +======================================================= + +Insert this code below your body tag in 'app/views/layouts/application.html.erb': + +-------- + <%= notice if notice %> + + <% if current_user.present? %> + <%= button_to 'Log Out', logout_path, to: 'pages#logout', method: :delete, id: 'logout_button' %> + <% else %> + <%= button_to 'Log in', login_path, method: :get, id: 'login_button' %> + <%= button_to 'Sign up', signup_path, method: :get, id: 'signup_button' %> + <% end %> +--------- + + - Notice for showing messages. + - Buttons for Log in, Sign up and Log out. + +=======================================================