Skip to content

Commit

Permalink
Associate tools to users (#2)
Browse files Browse the repository at this point in the history
* add user_id to tools, set up associations and factories, and annotate gem

* tool index with user contact info
  • Loading branch information
sharkby7e authored Oct 1, 2024
1 parent b5615b2 commit cbb5082
Show file tree
Hide file tree
Showing 14 changed files with 177 additions and 10 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ gem 'tzinfo-data', platforms: %i[windows jruby]

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem 'annotate'
gem 'debug', platforms: %i[mri mingw x64_mingw]
gem 'factory_bot_rails'
gem 'faker'
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ GEM
tzinfo (~> 2.0)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
annotate (3.2.0)
activerecord (>= 3.2, < 8.0)
rake (>= 10.4, < 14.0)
base64 (0.2.0)
bcrypt (3.1.20)
bigdecimal (3.1.8)
Expand Down Expand Up @@ -367,6 +370,7 @@ PLATFORMS
x86_64-linux-musl

DEPENDENCIES
annotate
bootsnap
capybara
debug
Expand Down
1 change: 1 addition & 0 deletions app/controllers/tools_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

class ToolsController < ApplicationController
def index
@tools = Tool.all
end
end
14 changes: 14 additions & 0 deletions app/models/tool.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
# frozen_string_literal: true

class Tool < ApplicationRecord
belongs_to :user
end

# == Schema Information
#
# Table name: tools
#
# id :integer not null, primary key
# brand_name :string
# description :text
# name :string
# created_at :datetime not null
# updated_at :datetime not null
# user_id :integer not null
#
35 changes: 35 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,39 @@ class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :confirmable,
:trackable, :omniauthable

has_many :tools
end

# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# confirmation_sent_at :datetime
# confirmation_token :string
# confirmed_at :datetime
# current_sign_in_at :datetime
# current_sign_in_ip :string
# email :string default(""), not null
# encrypted_password :string default(""), not null
# failed_attempts :integer default(0), not null
# last_sign_in_at :datetime
# last_sign_in_ip :string
# locked_at :datetime
# remember_created_at :datetime
# reset_password_sent_at :datetime
# reset_password_token :string
# sign_in_count :integer default(0), not null
# unconfirmed_email :string
# unlock_token :string
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_users_on_confirmation_token (confirmation_token) UNIQUE
# index_users_on_email (email) UNIQUE
# index_users_on_reset_password_token (reset_password_token) UNIQUE
# index_users_on_unlock_token (unlock_token) UNIQUE
#
23 changes: 22 additions & 1 deletion app/views/tools/index.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
<div class="text-3xl font-bold">Tools</div>
<div class="flex flex-col mx-auto w-3/4 gap-4">
<div class="text-3xl font-bold">Tools</div>
<table class='w-full'>
<thead class='text-left'>
<tr>
<th class='p-2 border border-slate-700'>Tool Name</th>
<th class='p-2 border border-slate-700'>Tool Brand</th>
<th class='p-2 border border-slate-700'>Owner Contact</th>
</tr>
</thead>

<tbody>
<% @tools.each do |tool| %>
<tr>
<td class='border border-slate-700 p-2'><%= tool.name %></td>
<td class='border border-slate-700 p-2'><%= tool.brand_name %></td>
<td class='border border-slate-700 p-2 underline'><%= mail_to tool.user.email %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
9 changes: 6 additions & 3 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require "active_support/core_ext/integer/time"
# frozen_string_literal: true

require 'active_support/core_ext/integer/time'

# The test environment is used exclusively to run your application's
# test suite. You never need to work with it otherwise. Remember that
Expand All @@ -15,12 +17,12 @@
# this is usually not necessary, and can slow down your test suite. However, it's
# recommended that you enable it in continuous integration systems to ensure eager
# loading is working properly before deploying your code.
config.eager_load = ENV["CI"].present?
config.eager_load = ENV['CI'].present?

# Configure public file server for tests with Cache-Control for performance.
config.public_file_server.enabled = true
config.public_file_server.headers = {
"Cache-Control" => "public, max-age=#{1.hour.to_i}"
'Cache-Control' => "public, max-age=#{1.hour.to_i}"
}

# Show full error reports and disable caching.
Expand All @@ -43,6 +45,7 @@
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test
Rails.application.routes.default_url_options[:host] = 'domain.example'

# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr
Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20241001014453_add_user_id_to_tools.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddUserIdToTools < ActiveRecord::Migration[7.1]
def change
add_column :tools, :user_id, :integer, null: false
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions lib/tasks/auto_annotate_models.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

# NOTE: only doing this in development as some production environments (Heroku)
# NOTE: are sensitive to local FS writes, and besides -- it's just not proper
# NOTE: to have a dev-mode tool do its thing in production.
if Rails.env.development?
require 'annotate'
task :set_annotation_options do
# You can override any of these by setting an environment variable of the
# same name.
Annotate.set_defaults(
'active_admin' => 'false',
'additional_file_patterns' => [],
'routes' => 'false',
'models' => 'true',
'position_in_routes' => 'before',
'position_in_class' => 'after',
'position_in_test' => 'before',
'position_in_fixture' => 'before',
'position_in_factory' => 'before',
'position_in_serializer' => 'before',
'show_foreign_keys' => 'true',
'show_complete_foreign_keys' => 'false',
'show_indexes' => 'true',
'simple_indexes' => 'false',
'model_dir' => 'app/models',
'root_dir' => '',
'include_version' => 'false',
'require' => '',
'exclude_tests' => 'true',
'exclude_fixtures' => 'false',
'exclude_factories' => 'false',
'exclude_serializers' => 'false',
'exclude_scaffolds' => 'true',
'exclude_controllers' => 'true',
'exclude_helpers' => 'true',
'exclude_sti_subclasses' => 'false',
'ignore_model_sub_dir' => 'false',
'ignore_columns' => nil,
'ignore_routes' => nil,
'ignore_unknown_models' => 'false',
'hide_limit_column_types' => 'integer,bigint,boolean',
'hide_default_column_types' => 'json,jsonb,hstore',
'skip_on_db_migrate' => 'false',
'format_bare' => 'true',
'format_rdoc' => 'false',
'format_yard' => 'false',
'format_markdown' => 'false',
'sort' => 'false',
'force' => 'false',
'frozen' => 'false',
'classified_sort' => 'true',
'trace' => 'false',
'wrapper_open' => nil,
'wrapper_close' => nil,
'with_comment' => 'true'
)
end

Annotate.load_tasks
end
16 changes: 13 additions & 3 deletions spec/controllers/tools_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@

require 'rails_helper'

RSpec.describe ToolsController, type: :controller do
describe 'GET /index' do
RSpec.describe ToolsController, type: :request do
describe '#index' do
let!(:tool) { create(:tool) }

it 'works' do
get :index
get tools_path

expect(response).to be_successful
end

it 'includes info about the tool' do
get tools_path

expect(response).to be_successful
expect(response.body).to have_content(tool.name)
expect(response.body).to have_content tool.user.email
end
end
end
4 changes: 3 additions & 1 deletion spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

FactoryBot.define do
factory :user do

email { '[email protected]' }
password { 'catnip' }
end

factory(:tool) do
association :user
name { 'Hammer' }
brand_name { 'Makita' }
end
Expand Down
6 changes: 5 additions & 1 deletion spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe User, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
it 'has a valid factory' do
expect(build(:user)).to be_valid
end
end
3 changes: 3 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
# https://rspec.info/features/6-0/rspec-rails
config.infer_spec_type_from_file_location!

# have_content etc...
config.include Capybara::RSpecMatchers, type: :request

# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
Expand Down

0 comments on commit cbb5082

Please sign in to comment.