From 66e4370fdba8cf16442fe7be65a42eb49e5e4de4 Mon Sep 17 00:00:00 2001 From: hassanShakur Date: Tue, 17 Oct 2023 23:01:35 +0300 Subject: [PATCH 01/39] add devise --- Gemfile | 2 ++ Gemfile.lock | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/Gemfile b/Gemfile index b559af4..c5be691 100644 --- a/Gemfile +++ b/Gemfile @@ -70,3 +70,5 @@ group :test do gem "selenium-webdriver" end + +gem "devise", "~> 4.9" diff --git a/Gemfile.lock b/Gemfile.lock index 700b38f..56531a6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -68,6 +68,7 @@ GEM tzinfo (~> 2.0) addressable (2.8.5) public_suffix (>= 2.0.2, < 6.0) + bcrypt (3.1.19) bindex (0.8.1) bootsnap (1.16.0) msgpack (~> 1.2) @@ -87,6 +88,12 @@ GEM debug (1.8.0) irb (>= 1.5.0) reline (>= 0.3.1) + devise (4.9.3) + bcrypt (~> 3.0) + orm_adapter (~> 0.1) + railties (>= 4.1.0) + responders + warden (~> 1.2.3) erubi (1.12.0) globalid (1.2.1) activesupport (>= 6.1) @@ -128,6 +135,7 @@ GEM nio4r (2.5.9) nokogiri (1.15.4-x64-mingw-ucrt) racc (~> 1.4) + orm_adapter (0.5.0) pg (1.5.4-x64-mingw-ucrt) psych (5.1.1.1) stringio @@ -172,6 +180,9 @@ GEM regexp_parser (2.8.2) reline (0.3.9) io-console (~> 0.5) + responders (3.1.1) + actionpack (>= 5.2) + railties (>= 5.2) rexml (3.2.6) rubyzip (2.3.2) selenium-webdriver (4.14.0) @@ -198,6 +209,8 @@ GEM concurrent-ruby (~> 1.0) tzinfo-data (1.2023.3) tzinfo (>= 1.0.0) + warden (1.2.9) + rack (>= 2.0.9) web-console (4.2.1) actionview (>= 6.0.0) activemodel (>= 6.0.0) @@ -218,6 +231,7 @@ DEPENDENCIES bootsnap capybara debug + devise (~> 4.9) importmap-rails jbuilder pg (~> 1.1) From 854012e834d07176eb566a1128f34a52a2baeb96 Mon Sep 17 00:00:00 2001 From: hassanShakur Date: Tue, 17 Oct 2023 23:10:41 +0300 Subject: [PATCH 02/39] setup devise & User model --- app/controllers/users_controller.rb | 70 ++++ app/helpers/users_helper.rb | 2 + app/models/user.rb | 6 + app/views/devise/confirmations/new.html.erb | 16 + .../mailer/confirmation_instructions.html.erb | 5 + .../devise/mailer/email_changed.html.erb | 7 + .../devise/mailer/password_change.html.erb | 3 + .../reset_password_instructions.html.erb | 8 + .../mailer/unlock_instructions.html.erb | 7 + app/views/devise/passwords/edit.html.erb | 25 ++ app/views/devise/passwords/new.html.erb | 16 + app/views/devise/registrations/edit.html.erb | 43 +++ app/views/devise/registrations/new.html.erb | 29 ++ app/views/devise/sessions/new.html.erb | 26 ++ .../devise/shared/_error_messages.html.erb | 15 + app/views/devise/shared/_links.html.erb | 25 ++ app/views/devise/unlocks/new.html.erb | 16 + app/views/layouts/application.html.erb | 3 + app/views/users/_form.html.erb | 22 ++ app/views/users/_user.html.erb | 7 + app/views/users/_user.json.jbuilder | 2 + app/views/users/edit.html.erb | 10 + app/views/users/index.html.erb | 14 + app/views/users/index.json.jbuilder | 1 + app/views/users/new.html.erb | 9 + app/views/users/show.html.erb | 10 + app/views/users/show.json.jbuilder | 1 + config/environments/development.rb | 2 + config/initializers/devise.rb | 313 ++++++++++++++++++ config/locales/devise.en.yml | 65 ++++ config/routes.rb | 4 + db/migrate/20231017200507_create_users.rb | 9 + .../20231017200833_add_devise_to_users.rb | 51 +++ test/controllers/users_controller_test.rb | 48 +++ test/fixtures/users.yml | 7 + test/models/user_test.rb | 7 + test/system/users_test.rb | 41 +++ 37 files changed, 945 insertions(+) create mode 100644 app/controllers/users_controller.rb create mode 100644 app/helpers/users_helper.rb create mode 100644 app/models/user.rb create mode 100644 app/views/devise/confirmations/new.html.erb create mode 100644 app/views/devise/mailer/confirmation_instructions.html.erb create mode 100644 app/views/devise/mailer/email_changed.html.erb create mode 100644 app/views/devise/mailer/password_change.html.erb create mode 100644 app/views/devise/mailer/reset_password_instructions.html.erb create mode 100644 app/views/devise/mailer/unlock_instructions.html.erb create mode 100644 app/views/devise/passwords/edit.html.erb create mode 100644 app/views/devise/passwords/new.html.erb create mode 100644 app/views/devise/registrations/edit.html.erb create mode 100644 app/views/devise/registrations/new.html.erb create mode 100644 app/views/devise/sessions/new.html.erb create mode 100644 app/views/devise/shared/_error_messages.html.erb create mode 100644 app/views/devise/shared/_links.html.erb create mode 100644 app/views/devise/unlocks/new.html.erb create mode 100644 app/views/users/_form.html.erb create mode 100644 app/views/users/_user.html.erb create mode 100644 app/views/users/_user.json.jbuilder create mode 100644 app/views/users/edit.html.erb create mode 100644 app/views/users/index.html.erb create mode 100644 app/views/users/index.json.jbuilder create mode 100644 app/views/users/new.html.erb create mode 100644 app/views/users/show.html.erb create mode 100644 app/views/users/show.json.jbuilder create mode 100644 config/initializers/devise.rb create mode 100644 config/locales/devise.en.yml create mode 100644 db/migrate/20231017200507_create_users.rb create mode 100644 db/migrate/20231017200833_add_devise_to_users.rb create mode 100644 test/controllers/users_controller_test.rb create mode 100644 test/fixtures/users.yml create mode 100644 test/models/user_test.rb create mode 100644 test/system/users_test.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000..279094d --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,70 @@ +class UsersController < ApplicationController + before_action :set_user, only: %i[ show edit update destroy ] + + # GET /users or /users.json + def index + @users = User.all + end + + # GET /users/1 or /users/1.json + def show + end + + # GET /users/new + def new + @user = User.new + end + + # GET /users/1/edit + def edit + end + + # POST /users or /users.json + def create + @user = User.new(user_params) + + respond_to do |format| + if @user.save + format.html { redirect_to user_url(@user), notice: "User was successfully created." } + format.json { render :show, status: :created, location: @user } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @user.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /users/1 or /users/1.json + def update + respond_to do |format| + if @user.update(user_params) + format.html { redirect_to user_url(@user), notice: "User was successfully updated." } + format.json { render :show, status: :ok, location: @user } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @user.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /users/1 or /users/1.json + def destroy + @user.destroy + + respond_to do |format| + format.html { redirect_to users_url, notice: "User was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_user + @user = User.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def user_params + params.require(:user).permit(:name) + end +end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb new file mode 100644 index 0000000..2310a24 --- /dev/null +++ b/app/helpers/users_helper.rb @@ -0,0 +1,2 @@ +module UsersHelper +end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..4756799 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,6 @@ +class User < ApplicationRecord + # Include default devise modules. Others available are: + # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable + devise :database_authenticatable, :registerable, + :recoverable, :rememberable, :validatable +end diff --git a/app/views/devise/confirmations/new.html.erb b/app/views/devise/confirmations/new.html.erb new file mode 100644 index 0000000..b12dd0c --- /dev/null +++ b/app/views/devise/confirmations/new.html.erb @@ -0,0 +1,16 @@ +

Resend confirmation instructions

+ +<%= form_for(resource, as: resource_name, url: confirmation_path(resource_name), html: { method: :post }) do |f| %> + <%= render "devise/shared/error_messages", resource: resource %> + +
+ <%= f.label :email %>
+ <%= f.email_field :email, autofocus: true, autocomplete: "email", value: (resource.pending_reconfirmation? ? resource.unconfirmed_email : resource.email) %> +
+ +
+ <%= f.submit "Resend confirmation instructions" %> +
+<% end %> + +<%= render "devise/shared/links" %> diff --git a/app/views/devise/mailer/confirmation_instructions.html.erb b/app/views/devise/mailer/confirmation_instructions.html.erb new file mode 100644 index 0000000..dc55f64 --- /dev/null +++ b/app/views/devise/mailer/confirmation_instructions.html.erb @@ -0,0 +1,5 @@ +

Welcome <%= @email %>!

+ +

You can confirm your account email through the link below:

+ +

<%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %>

diff --git a/app/views/devise/mailer/email_changed.html.erb b/app/views/devise/mailer/email_changed.html.erb new file mode 100644 index 0000000..32f4ba8 --- /dev/null +++ b/app/views/devise/mailer/email_changed.html.erb @@ -0,0 +1,7 @@ +

Hello <%= @email %>!

+ +<% if @resource.try(:unconfirmed_email?) %> +

We're contacting you to notify you that your email is being changed to <%= @resource.unconfirmed_email %>.

+<% else %> +

We're contacting you to notify you that your email has been changed to <%= @resource.email %>.

+<% end %> diff --git a/app/views/devise/mailer/password_change.html.erb b/app/views/devise/mailer/password_change.html.erb new file mode 100644 index 0000000..b41daf4 --- /dev/null +++ b/app/views/devise/mailer/password_change.html.erb @@ -0,0 +1,3 @@ +

Hello <%= @resource.email %>!

+ +

We're contacting you to notify you that your password has been changed.

diff --git a/app/views/devise/mailer/reset_password_instructions.html.erb b/app/views/devise/mailer/reset_password_instructions.html.erb new file mode 100644 index 0000000..f667dc1 --- /dev/null +++ b/app/views/devise/mailer/reset_password_instructions.html.erb @@ -0,0 +1,8 @@ +

Hello <%= @resource.email %>!

+ +

Someone has requested a link to change your password. You can do this through the link below.

+ +

<%= link_to 'Change my password', edit_password_url(@resource, reset_password_token: @token) %>

+ +

If you didn't request this, please ignore this email.

+

Your password won't change until you access the link above and create a new one.

diff --git a/app/views/devise/mailer/unlock_instructions.html.erb b/app/views/devise/mailer/unlock_instructions.html.erb new file mode 100644 index 0000000..41e148b --- /dev/null +++ b/app/views/devise/mailer/unlock_instructions.html.erb @@ -0,0 +1,7 @@ +

Hello <%= @resource.email %>!

+ +

Your account has been locked due to an excessive number of unsuccessful sign in attempts.

+ +

Click the link below to unlock your account:

+ +

<%= link_to 'Unlock my account', unlock_url(@resource, unlock_token: @token) %>

diff --git a/app/views/devise/passwords/edit.html.erb b/app/views/devise/passwords/edit.html.erb new file mode 100644 index 0000000..5fbb9ff --- /dev/null +++ b/app/views/devise/passwords/edit.html.erb @@ -0,0 +1,25 @@ +

Change your password

+ +<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :put }) do |f| %> + <%= render "devise/shared/error_messages", resource: resource %> + <%= f.hidden_field :reset_password_token %> + +
+ <%= f.label :password, "New password" %>
+ <% if @minimum_password_length %> + (<%= @minimum_password_length %> characters minimum)
+ <% end %> + <%= f.password_field :password, autofocus: true, autocomplete: "new-password" %> +
+ +
+ <%= f.label :password_confirmation, "Confirm new password" %>
+ <%= f.password_field :password_confirmation, autocomplete: "new-password" %> +
+ +
+ <%= f.submit "Change my password" %> +
+<% end %> + +<%= render "devise/shared/links" %> diff --git a/app/views/devise/passwords/new.html.erb b/app/views/devise/passwords/new.html.erb new file mode 100644 index 0000000..9b486b8 --- /dev/null +++ b/app/views/devise/passwords/new.html.erb @@ -0,0 +1,16 @@ +

Forgot your password?

+ +<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :post }) do |f| %> + <%= render "devise/shared/error_messages", resource: resource %> + +
+ <%= f.label :email %>
+ <%= f.email_field :email, autofocus: true, autocomplete: "email" %> +
+ +
+ <%= f.submit "Send me reset password instructions" %> +
+<% end %> + +<%= render "devise/shared/links" %> diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/edit.html.erb new file mode 100644 index 0000000..b82e336 --- /dev/null +++ b/app/views/devise/registrations/edit.html.erb @@ -0,0 +1,43 @@ +

Edit <%= resource_name.to_s.humanize %>

+ +<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %> + <%= render "devise/shared/error_messages", resource: resource %> + +
+ <%= f.label :email %>
+ <%= f.email_field :email, autofocus: true, autocomplete: "email" %> +
+ + <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %> +
Currently waiting confirmation for: <%= resource.unconfirmed_email %>
+ <% end %> + +
+ <%= f.label :password %> (leave blank if you don't want to change it)
+ <%= f.password_field :password, autocomplete: "new-password" %> + <% if @minimum_password_length %> +
+ <%= @minimum_password_length %> characters minimum + <% end %> +
+ +
+ <%= f.label :password_confirmation %>
+ <%= f.password_field :password_confirmation, autocomplete: "new-password" %> +
+ +
+ <%= f.label :current_password %> (we need your current password to confirm your changes)
+ <%= f.password_field :current_password, autocomplete: "current-password" %> +
+ +
+ <%= f.submit "Update" %> +
+<% end %> + +

Cancel my account

+ +
Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?", turbo_confirm: "Are you sure?" }, method: :delete %>
+ +<%= link_to "Back", :back %> diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb new file mode 100644 index 0000000..d655b66 --- /dev/null +++ b/app/views/devise/registrations/new.html.erb @@ -0,0 +1,29 @@ +

Sign up

+ +<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> + <%= render "devise/shared/error_messages", resource: resource %> + +
+ <%= f.label :email %>
+ <%= f.email_field :email, autofocus: true, autocomplete: "email" %> +
+ +
+ <%= f.label :password %> + <% if @minimum_password_length %> + (<%= @minimum_password_length %> characters minimum) + <% end %>
+ <%= f.password_field :password, autocomplete: "new-password" %> +
+ +
+ <%= f.label :password_confirmation %>
+ <%= f.password_field :password_confirmation, autocomplete: "new-password" %> +
+ +
+ <%= f.submit "Sign up" %> +
+<% end %> + +<%= render "devise/shared/links" %> diff --git a/app/views/devise/sessions/new.html.erb b/app/views/devise/sessions/new.html.erb new file mode 100644 index 0000000..5ede964 --- /dev/null +++ b/app/views/devise/sessions/new.html.erb @@ -0,0 +1,26 @@ +

Log in

+ +<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> +
+ <%= f.label :email %>
+ <%= f.email_field :email, autofocus: true, autocomplete: "email" %> +
+ +
+ <%= f.label :password %>
+ <%= f.password_field :password, autocomplete: "current-password" %> +
+ + <% if devise_mapping.rememberable? %> +
+ <%= f.check_box :remember_me %> + <%= f.label :remember_me %> +
+ <% end %> + +
+ <%= f.submit "Log in" %> +
+<% end %> + +<%= render "devise/shared/links" %> diff --git a/app/views/devise/shared/_error_messages.html.erb b/app/views/devise/shared/_error_messages.html.erb new file mode 100644 index 0000000..cabfe30 --- /dev/null +++ b/app/views/devise/shared/_error_messages.html.erb @@ -0,0 +1,15 @@ +<% if resource.errors.any? %> +
+

+ <%= I18n.t("errors.messages.not_saved", + count: resource.errors.count, + resource: resource.class.model_name.human.downcase) + %> +

+
    + <% resource.errors.full_messages.each do |message| %> +
  • <%= message %>
  • + <% end %> +
+
+<% end %> diff --git a/app/views/devise/shared/_links.html.erb b/app/views/devise/shared/_links.html.erb new file mode 100644 index 0000000..7a75304 --- /dev/null +++ b/app/views/devise/shared/_links.html.erb @@ -0,0 +1,25 @@ +<%- if controller_name != 'sessions' %> + <%= link_to "Log in", new_session_path(resource_name) %>
+<% end %> + +<%- if devise_mapping.registerable? && controller_name != 'registrations' %> + <%= link_to "Sign up", new_registration_path(resource_name) %>
+<% end %> + +<%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %> + <%= link_to "Forgot your password?", new_password_path(resource_name) %>
+<% end %> + +<%- if devise_mapping.confirmable? && controller_name != 'confirmations' %> + <%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %>
+<% end %> + +<%- if devise_mapping.lockable? && resource_class.unlock_strategy_enabled?(:email) && controller_name != 'unlocks' %> + <%= link_to "Didn't receive unlock instructions?", new_unlock_path(resource_name) %>
+<% end %> + +<%- if devise_mapping.omniauthable? %> + <%- resource_class.omniauth_providers.each do |provider| %> + <%= button_to "Sign in with #{OmniAuth::Utils.camelize(provider)}", omniauth_authorize_path(resource_name, provider), data: { turbo: false } %>
+ <% end %> +<% end %> diff --git a/app/views/devise/unlocks/new.html.erb b/app/views/devise/unlocks/new.html.erb new file mode 100644 index 0000000..ffc34de --- /dev/null +++ b/app/views/devise/unlocks/new.html.erb @@ -0,0 +1,16 @@ +

Resend unlock instructions

+ +<%= form_for(resource, as: resource_name, url: unlock_path(resource_name), html: { method: :post }) do |f| %> + <%= render "devise/shared/error_messages", resource: resource %> + +
+ <%= f.label :email %>
+ <%= f.email_field :email, autofocus: true, autocomplete: "email" %> +
+ +
+ <%= f.submit "Resend unlock instructions" %> +
+<% end %> + +<%= render "devise/shared/links" %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 03fc2f6..0d9af08 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -11,6 +11,9 @@ +

<%= notice %>

+

<%= alert %>

+ <%= yield %> diff --git a/app/views/users/_form.html.erb b/app/views/users/_form.html.erb new file mode 100644 index 0000000..e35c1f2 --- /dev/null +++ b/app/views/users/_form.html.erb @@ -0,0 +1,22 @@ +<%= form_with(model: user) do |form| %> + <% if user.errors.any? %> +
+

<%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:

+ +
    + <% user.errors.each do |error| %> +
  • <%= error.full_message %>
  • + <% end %> +
+
+ <% end %> + +
+ <%= form.label :name, style: "display: block" %> + <%= form.text_field :name %> +
+ +
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/users/_user.html.erb b/app/views/users/_user.html.erb new file mode 100644 index 0000000..5d2c950 --- /dev/null +++ b/app/views/users/_user.html.erb @@ -0,0 +1,7 @@ +
+

+ Name: + <%= user.name %> +

+ +
diff --git a/app/views/users/_user.json.jbuilder b/app/views/users/_user.json.jbuilder new file mode 100644 index 0000000..df793f1 --- /dev/null +++ b/app/views/users/_user.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! user, :id, :name, :created_at, :updated_at +json.url user_url(user, format: :json) diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb new file mode 100644 index 0000000..9dda632 --- /dev/null +++ b/app/views/users/edit.html.erb @@ -0,0 +1,10 @@ +

Editing user

+ +<%= render "form", user: @user %> + +
+ +
+ <%= link_to "Show this user", @user %> | + <%= link_to "Back to users", users_path %> +
diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb new file mode 100644 index 0000000..fe4dd5c --- /dev/null +++ b/app/views/users/index.html.erb @@ -0,0 +1,14 @@ +

<%= notice %>

+ +

Users

+ +
+ <% @users.each do |user| %> + <%= render user %> +

+ <%= link_to "Show this user", user %> +

+ <% end %> +
+ +<%= link_to "New user", new_user_path %> diff --git a/app/views/users/index.json.jbuilder b/app/views/users/index.json.jbuilder new file mode 100644 index 0000000..98788da --- /dev/null +++ b/app/views/users/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @users, partial: "users/user", as: :user diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb new file mode 100644 index 0000000..eedbd83 --- /dev/null +++ b/app/views/users/new.html.erb @@ -0,0 +1,9 @@ +

New user

+ +<%= render "form", user: @user %> + +
+ +
+ <%= link_to "Back to users", users_path %> +
diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb new file mode 100644 index 0000000..673fae2 --- /dev/null +++ b/app/views/users/show.html.erb @@ -0,0 +1,10 @@ +

<%= notice %>

+ +<%= render @user %> + +
+ <%= link_to "Edit this user", edit_user_path(@user) %> | + <%= link_to "Back to users", users_path %> + + <%= button_to "Destroy this user", @user, method: :delete %> +
diff --git a/app/views/users/show.json.jbuilder b/app/views/users/show.json.jbuilder new file mode 100644 index 0000000..ff40bb9 --- /dev/null +++ b/app/views/users/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "users/user", user: @user diff --git a/config/environments/development.rb b/config/environments/development.rb index 8500f45..494e2ed 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -41,6 +41,8 @@ config.action_mailer.perform_caching = false + config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } + # Print deprecation notices to the Rails logger. config.active_support.deprecation = :log diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb new file mode 100644 index 0000000..3ad6cfa --- /dev/null +++ b/config/initializers/devise.rb @@ -0,0 +1,313 @@ +# frozen_string_literal: true + +# Assuming you have not yet modified this file, each configuration option below +# is set to its default value. Note that some are commented out while others +# are not: uncommented lines are intended to protect your configuration from +# breaking changes in upgrades (i.e., in the event that future versions of +# Devise change the default values for those options). +# +# Use this hook to configure devise mailer, warden hooks and so forth. +# Many of these configuration options can be set straight in your model. +Devise.setup do |config| + # The secret key used by Devise. Devise uses this key to generate + # random tokens. Changing this key will render invalid all existing + # confirmation, reset password and unlock tokens in the database. + # Devise will use the `secret_key_base` as its `secret_key` + # by default. You can change it below and use your own secret key. + # config.secret_key = '847f08822358ca24bce4421fb4cfb7c5cc598a1e785888902e0a6083549e6018052ad6417db77c7b7fbf25d05af5d6a129460f43e7abe1b3eca4c8f2908e1ec5' + + # ==> Controller configuration + # Configure the parent class to the devise controllers. + # config.parent_controller = 'DeviseController' + + # ==> Mailer Configuration + # Configure the e-mail address which will be shown in Devise::Mailer, + # note that it will be overwritten if you use your own mailer class + # with default "from" parameter. + config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com' + + # Configure the class responsible to send e-mails. + # config.mailer = 'Devise::Mailer' + + # Configure the parent class responsible to send e-mails. + # config.parent_mailer = 'ActionMailer::Base' + + # ==> ORM configuration + # Load and configure the ORM. Supports :active_record (default) and + # :mongoid (bson_ext recommended) by default. Other ORMs may be + # available as additional gems. + require 'devise/orm/active_record' + + # ==> Configuration for any authentication mechanism + # Configure which keys are used when authenticating a user. The default is + # just :email. You can configure it to use [:username, :subdomain], so for + # authenticating a user, both parameters are required. Remember that those + # parameters are used only when authenticating and not when retrieving from + # session. If you need permissions, you should implement that in a before filter. + # You can also supply a hash where the value is a boolean determining whether + # or not authentication should be aborted when the value is not present. + # config.authentication_keys = [:email] + + # Configure parameters from the request object used for authentication. Each entry + # given should be a request method and it will automatically be passed to the + # find_for_authentication method and considered in your model lookup. For instance, + # if you set :request_keys to [:subdomain], :subdomain will be used on authentication. + # The same considerations mentioned for authentication_keys also apply to request_keys. + # config.request_keys = [] + + # Configure which authentication keys should be case-insensitive. + # These keys will be downcased upon creating or modifying a user and when used + # to authenticate or find a user. Default is :email. + config.case_insensitive_keys = [:email] + + # Configure which authentication keys should have whitespace stripped. + # These keys will have whitespace before and after removed upon creating or + # modifying a user and when used to authenticate or find a user. Default is :email. + config.strip_whitespace_keys = [:email] + + # Tell if authentication through request.params is enabled. True by default. + # It can be set to an array that will enable params authentication only for the + # given strategies, for example, `config.params_authenticatable = [:database]` will + # enable it only for database (email + password) authentication. + # config.params_authenticatable = true + + # Tell if authentication through HTTP Auth is enabled. False by default. + # It can be set to an array that will enable http authentication only for the + # given strategies, for example, `config.http_authenticatable = [:database]` will + # enable it only for database authentication. + # For API-only applications to support authentication "out-of-the-box", you will likely want to + # enable this with :database unless you are using a custom strategy. + # The supported strategies are: + # :database = Support basic authentication with authentication key + password + # config.http_authenticatable = false + + # If 401 status code should be returned for AJAX requests. True by default. + # config.http_authenticatable_on_xhr = true + + # The realm used in Http Basic Authentication. 'Application' by default. + # config.http_authentication_realm = 'Application' + + # It will change confirmation, password recovery and other workflows + # to behave the same regardless if the e-mail provided was right or wrong. + # Does not affect registerable. + # config.paranoid = true + + # By default Devise will store the user in session. You can skip storage for + # particular strategies by setting this option. + # Notice that if you are skipping storage for all authentication paths, you + # may want to disable generating routes to Devise's sessions controller by + # passing skip: :sessions to `devise_for` in your config/routes.rb + config.skip_session_storage = [:http_auth] + + # By default, Devise cleans up the CSRF token on authentication to + # avoid CSRF token fixation attacks. This means that, when using AJAX + # requests for sign in and sign up, you need to get a new CSRF token + # from the server. You can disable this option at your own risk. + # config.clean_up_csrf_token_on_authentication = true + + # When false, Devise will not attempt to reload routes on eager load. + # This can reduce the time taken to boot the app but if your application + # requires the Devise mappings to be loaded during boot time the application + # won't boot properly. + # config.reload_routes = true + + # ==> Configuration for :database_authenticatable + # For bcrypt, this is the cost for hashing the password and defaults to 12. If + # using other algorithms, it sets how many times you want the password to be hashed. + # The number of stretches used for generating the hashed password are stored + # with the hashed password. This allows you to change the stretches without + # invalidating existing passwords. + # + # Limiting the stretches to just one in testing will increase the performance of + # your test suite dramatically. However, it is STRONGLY RECOMMENDED to not use + # a value less than 10 in other environments. Note that, for bcrypt (the default + # algorithm), the cost increases exponentially with the number of stretches (e.g. + # a value of 20 is already extremely slow: approx. 60 seconds for 1 calculation). + config.stretches = Rails.env.test? ? 1 : 12 + + # Set up a pepper to generate the hashed password. + # config.pepper = '5608bddcddb0e1ffd2f5bfe55c8829c922f4320ebc4a64cc15862bdbd0d81f6b46eb60bdb45e3c438f6a85cadc3ea75b95b944c8aa6faeb09334fd80f22f283d' + + # Send a notification to the original email when the user's email is changed. + # config.send_email_changed_notification = false + + # Send a notification email when the user's password is changed. + # config.send_password_change_notification = false + + # ==> Configuration for :confirmable + # A period that the user is allowed to access the website even without + # confirming their account. For instance, if set to 2.days, the user will be + # able to access the website for two days without confirming their account, + # access will be blocked just in the third day. + # You can also set it to nil, which will allow the user to access the website + # without confirming their account. + # Default is 0.days, meaning the user cannot access the website without + # confirming their account. + # config.allow_unconfirmed_access_for = 2.days + + # A period that the user is allowed to confirm their account before their + # token becomes invalid. For example, if set to 3.days, the user can confirm + # their account within 3 days after the mail was sent, but on the fourth day + # their account can't be confirmed with the token any more. + # Default is nil, meaning there is no restriction on how long a user can take + # before confirming their account. + # config.confirm_within = 3.days + + # If true, requires any email changes to be confirmed (exactly the same way as + # initial account confirmation) to be applied. Requires additional unconfirmed_email + # db field (see migrations). Until confirmed, new email is stored in + # unconfirmed_email column, and copied to email column on successful confirmation. + config.reconfirmable = true + + # Defines which key will be used when confirming an account + # config.confirmation_keys = [:email] + + # ==> Configuration for :rememberable + # The time the user will be remembered without asking for credentials again. + # config.remember_for = 2.weeks + + # Invalidates all the remember me tokens when the user signs out. + config.expire_all_remember_me_on_sign_out = true + + # If true, extends the user's remember period when remembered via cookie. + # config.extend_remember_period = false + + # Options to be passed to the created cookie. For instance, you can set + # secure: true in order to force SSL only cookies. + # config.rememberable_options = {} + + # ==> Configuration for :validatable + # Range for password length. + config.password_length = 6..128 + + # Email regex used to validate email formats. It simply asserts that + # one (and only one) @ exists in the given string. This is mainly + # to give user feedback and not to assert the e-mail validity. + config.email_regexp = /\A[^@\s]+@[^@\s]+\z/ + + # ==> Configuration for :timeoutable + # The time you want to timeout the user session without activity. After this + # time the user will be asked for credentials again. Default is 30 minutes. + # config.timeout_in = 30.minutes + + # ==> Configuration for :lockable + # Defines which strategy will be used to lock an account. + # :failed_attempts = Locks an account after a number of failed attempts to sign in. + # :none = No lock strategy. You should handle locking by yourself. + # config.lock_strategy = :failed_attempts + + # Defines which key will be used when locking and unlocking an account + # config.unlock_keys = [:email] + + # Defines which strategy will be used to unlock an account. + # :email = Sends an unlock link to the user email + # :time = Re-enables login after a certain amount of time (see :unlock_in below) + # :both = Enables both strategies + # :none = No unlock strategy. You should handle unlocking by yourself. + # config.unlock_strategy = :both + + # Number of authentication tries before locking an account if lock_strategy + # is failed attempts. + # config.maximum_attempts = 20 + + # Time interval to unlock the account if :time is enabled as unlock_strategy. + # config.unlock_in = 1.hour + + # Warn on the last attempt before the account is locked. + # config.last_attempt_warning = true + + # ==> Configuration for :recoverable + # + # Defines which key will be used when recovering the password for an account + # config.reset_password_keys = [:email] + + # Time interval you can reset your password with a reset password key. + # Don't put a too small interval or your users won't have the time to + # change their passwords. + config.reset_password_within = 6.hours + + # When set to false, does not sign a user in automatically after their password is + # reset. Defaults to true, so a user is signed in automatically after a reset. + # config.sign_in_after_reset_password = true + + # ==> Configuration for :encryptable + # Allow you to use another hashing or encryption algorithm besides bcrypt (default). + # You can use :sha1, :sha512 or algorithms from others authentication tools as + # :clearance_sha1, :authlogic_sha512 (then you should set stretches above to 20 + # for default behavior) and :restful_authentication_sha1 (then you should set + # stretches to 10, and copy REST_AUTH_SITE_KEY to pepper). + # + # Require the `devise-encryptable` gem when using anything other than bcrypt + # config.encryptor = :sha512 + + # ==> Scopes configuration + # Turn scoped views on. Before rendering "sessions/new", it will first check for + # "users/sessions/new". It's turned off by default because it's slower if you + # are using only default views. + # config.scoped_views = false + + # Configure the default scope given to Warden. By default it's the first + # devise role declared in your routes (usually :user). + # config.default_scope = :user + + # Set this configuration to false if you want /users/sign_out to sign out + # only the current scope. By default, Devise signs out all scopes. + # config.sign_out_all_scopes = true + + # ==> Navigation configuration + # Lists the formats that should be treated as navigational. Formats like + # :html should redirect to the sign in page when the user does not have + # access, but formats like :xml or :json, should return 401. + # + # If you have any extra navigational formats, like :iphone or :mobile, you + # should add them to the navigational formats lists. + # + # The "*/*" below is required to match Internet Explorer requests. + # config.navigational_formats = ['*/*', :html, :turbo_stream] + + # The default HTTP method used to sign out a resource. Default is :delete. + config.sign_out_via = :delete + + # ==> OmniAuth + # Add a new OmniAuth provider. Check the wiki for more information on setting + # up on your models and hooks. + # config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo' + + # ==> Warden configuration + # If you want to use other strategies, that are not supported by Devise, or + # change the failure app, you can configure them inside the config.warden block. + # + # config.warden do |manager| + # manager.intercept_401 = false + # manager.default_strategies(scope: :user).unshift :some_external_strategy + # end + + # ==> Mountable engine configurations + # When using Devise inside an engine, let's call it `MyEngine`, and this engine + # is mountable, there are some extra configurations to be taken into account. + # The following options are available, assuming the engine is mounted as: + # + # mount MyEngine, at: '/my_engine' + # + # The router that invoked `devise_for`, in the example above, would be: + # config.router_name = :my_engine + # + # When using OmniAuth, Devise cannot automatically set OmniAuth path, + # so you need to do it manually. For the users scope, it would be: + # config.omniauth_path_prefix = '/my_engine/users/auth' + + # ==> Hotwire/Turbo configuration + # When using Devise with Hotwire/Turbo, the http status for error responses + # and some redirects must match the following. The default in Devise for existing + # apps is `200 OK` and `302 Found` respectively, but new apps are generated with + # these new defaults that match Hotwire/Turbo behavior. + # Note: These might become the new default in future versions of Devise. + config.responder.error_status = :unprocessable_entity + config.responder.redirect_status = :see_other + + # ==> Configuration for :registerable + + # When set to false, does not sign a user in automatically after their password is + # changed. Defaults to true, so a user is signed in automatically after changing a password. + # config.sign_in_after_change_password = true +end diff --git a/config/locales/devise.en.yml b/config/locales/devise.en.yml new file mode 100644 index 0000000..260e1c4 --- /dev/null +++ b/config/locales/devise.en.yml @@ -0,0 +1,65 @@ +# Additional translations at https://github.com/heartcombo/devise/wiki/I18n + +en: + devise: + confirmations: + confirmed: "Your email address has been successfully confirmed." + send_instructions: "You will receive an email with instructions for how to confirm your email address in a few minutes." + send_paranoid_instructions: "If your email address exists in our database, you will receive an email with instructions for how to confirm your email address in a few minutes." + failure: + already_authenticated: "You are already signed in." + inactive: "Your account is not activated yet." + invalid: "Invalid %{authentication_keys} or password." + locked: "Your account is locked." + last_attempt: "You have one more attempt before your account is locked." + not_found_in_database: "Invalid %{authentication_keys} or password." + timeout: "Your session expired. Please sign in again to continue." + unauthenticated: "You need to sign in or sign up before continuing." + unconfirmed: "You have to confirm your email address before continuing." + mailer: + confirmation_instructions: + subject: "Confirmation instructions" + reset_password_instructions: + subject: "Reset password instructions" + unlock_instructions: + subject: "Unlock instructions" + email_changed: + subject: "Email Changed" + password_change: + subject: "Password Changed" + omniauth_callbacks: + failure: "Could not authenticate you from %{kind} because \"%{reason}\"." + success: "Successfully authenticated from %{kind} account." + passwords: + no_token: "You can't access this page without coming from a password reset email. If you do come from a password reset email, please make sure you used the full URL provided." + send_instructions: "You will receive an email with instructions on how to reset your password in a few minutes." + send_paranoid_instructions: "If your email address exists in our database, you will receive a password recovery link at your email address in a few minutes." + updated: "Your password has been changed successfully. You are now signed in." + updated_not_active: "Your password has been changed successfully." + registrations: + destroyed: "Bye! Your account has been successfully cancelled. We hope to see you again soon." + signed_up: "Welcome! You have signed up successfully." + signed_up_but_inactive: "You have signed up successfully. However, we could not sign you in because your account is not yet activated." + signed_up_but_locked: "You have signed up successfully. However, we could not sign you in because your account is locked." + signed_up_but_unconfirmed: "A message with a confirmation link has been sent to your email address. Please follow the link to activate your account." + update_needs_confirmation: "You updated your account successfully, but we need to verify your new email address. Please check your email and follow the confirmation link to confirm your new email address." + updated: "Your account has been updated successfully." + updated_but_not_signed_in: "Your account has been updated successfully, but since your password was changed, you need to sign in again." + sessions: + signed_in: "Signed in successfully." + signed_out: "Signed out successfully." + already_signed_out: "Signed out successfully." + unlocks: + send_instructions: "You will receive an email with instructions for how to unlock your account in a few minutes." + send_paranoid_instructions: "If your account exists, you will receive an email with instructions for how to unlock it in a few minutes." + unlocked: "Your account has been unlocked successfully. Please sign in to continue." + errors: + messages: + already_confirmed: "was already confirmed, please try signing in" + confirmation_period_expired: "needs to be confirmed within %{period}, please request a new one" + expired: "has expired, please request a new one" + not_found: "not found" + not_locked: "was not locked" + not_saved: + one: "1 error prohibited this %{resource} from being saved:" + other: "%{count} errors prohibited this %{resource} from being saved:" diff --git a/config/routes.rb b/config/routes.rb index 262ffd5..3fbe355 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,8 @@ Rails.application.routes.draw do + devise_for :users + resources :users + + root 'users#index' # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Defines the root path route ("/") diff --git a/db/migrate/20231017200507_create_users.rb b/db/migrate/20231017200507_create_users.rb new file mode 100644 index 0000000..dd02b09 --- /dev/null +++ b/db/migrate/20231017200507_create_users.rb @@ -0,0 +1,9 @@ +class CreateUsers < ActiveRecord::Migration[7.0] + def change + create_table :users do |t| + t.string :name + + t.timestamps + end + end +end diff --git a/db/migrate/20231017200833_add_devise_to_users.rb b/db/migrate/20231017200833_add_devise_to_users.rb new file mode 100644 index 0000000..98ed9dc --- /dev/null +++ b/db/migrate/20231017200833_add_devise_to_users.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +class AddDeviseToUsers < ActiveRecord::Migration[7.0] + def self.up + change_table :users do |t| + ## Database authenticatable + t.string :email, null: false, default: "" + t.string :encrypted_password, null: false, default: "" + + ## Recoverable + t.string :reset_password_token + t.datetime :reset_password_sent_at + + ## Rememberable + t.datetime :remember_created_at + + ## Trackable + # t.integer :sign_in_count, default: 0, null: false + # t.datetime :current_sign_in_at + # t.datetime :last_sign_in_at + # t.string :current_sign_in_ip + # t.string :last_sign_in_ip + + ## Confirmable + # t.string :confirmation_token + # t.datetime :confirmed_at + # t.datetime :confirmation_sent_at + # t.string :unconfirmed_email # Only if using reconfirmable + + ## Lockable + # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts + # t.string :unlock_token # Only if unlock strategy is :email or :both + # t.datetime :locked_at + + + # Uncomment below if timestamps were not included in your original model. + # t.timestamps null: false + end + + add_index :users, :email, unique: true + add_index :users, :reset_password_token, unique: true + # add_index :users, :confirmation_token, unique: true + # add_index :users, :unlock_token, unique: true + end + + def self.down + # By default, we don't want to make any assumption about how to roll back a migration when your + # model already existed. Please edit below which fields you would like to remove in this migration. + raise ActiveRecord::IrreversibleMigration + end +end diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb new file mode 100644 index 0000000..7ebfb8f --- /dev/null +++ b/test/controllers/users_controller_test.rb @@ -0,0 +1,48 @@ +require "test_helper" + +class UsersControllerTest < ActionDispatch::IntegrationTest + setup do + @user = users(:one) + end + + test "should get index" do + get users_url + assert_response :success + end + + test "should get new" do + get new_user_url + assert_response :success + end + + test "should create user" do + assert_difference("User.count") do + post users_url, params: { user: { name: @user.name } } + end + + assert_redirected_to user_url(User.last) + end + + test "should show user" do + get user_url(@user) + assert_response :success + end + + test "should get edit" do + get edit_user_url(@user) + assert_response :success + end + + test "should update user" do + patch user_url(@user), params: { user: { name: @user.name } } + assert_redirected_to user_url(@user) + end + + test "should destroy user" do + assert_difference("User.count", -1) do + delete user_url(@user) + end + + assert_redirected_to users_url + end +end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 0000000..7d41224 --- /dev/null +++ b/test/fixtures/users.yml @@ -0,0 +1,7 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + name: MyString + +two: + name: MyString diff --git a/test/models/user_test.rb b/test/models/user_test.rb new file mode 100644 index 0000000..5c07f49 --- /dev/null +++ b/test/models/user_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class UserTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/system/users_test.rb b/test/system/users_test.rb new file mode 100644 index 0000000..3553c0d --- /dev/null +++ b/test/system/users_test.rb @@ -0,0 +1,41 @@ +require "application_system_test_case" + +class UsersTest < ApplicationSystemTestCase + setup do + @user = users(:one) + end + + test "visiting the index" do + visit users_url + assert_selector "h1", text: "Users" + end + + test "should create user" do + visit users_url + click_on "New user" + + fill_in "Name", with: @user.name + click_on "Create User" + + assert_text "User was successfully created" + click_on "Back" + end + + test "should update User" do + visit user_url(@user) + click_on "Edit this user", match: :first + + fill_in "Name", with: @user.name + click_on "Update User" + + assert_text "User was successfully updated" + click_on "Back" + end + + test "should destroy User" do + visit user_url(@user) + click_on "Destroy this user", match: :first + + assert_text "User was successfully destroyed" + end +end From 24f6e88ec883f58fe182baf4af314871ffca2836 Mon Sep 17 00:00:00 2001 From: hassanShakur Date: Tue, 17 Oct 2023 23:12:59 +0300 Subject: [PATCH 03/39] generate Entity model --- app/controllers/entities_controller.rb | 70 ++++++++++++++++++++ app/helpers/entities_helper.rb | 2 + app/models/entity.rb | 2 + app/models/user.rb | 2 + app/views/entities/_entity.html.erb | 12 ++++ app/views/entities/_entity.json.jbuilder | 2 + app/views/entities/_form.html.erb | 27 ++++++++ app/views/entities/edit.html.erb | 10 +++ app/views/entities/index.html.erb | 14 ++++ app/views/entities/index.json.jbuilder | 1 + app/views/entities/new.html.erb | 9 +++ app/views/entities/show.html.erb | 10 +++ app/views/entities/show.json.jbuilder | 1 + config/routes.rb | 1 + db/migrate/20231017201238_create_entities.rb | 10 +++ test/controllers/entities_controller_test.rb | 48 ++++++++++++++ test/fixtures/entities.yml | 9 +++ test/models/entity_test.rb | 7 ++ test/system/entities_test.rb | 43 ++++++++++++ 19 files changed, 280 insertions(+) create mode 100644 app/controllers/entities_controller.rb create mode 100644 app/helpers/entities_helper.rb create mode 100644 app/models/entity.rb create mode 100644 app/views/entities/_entity.html.erb create mode 100644 app/views/entities/_entity.json.jbuilder create mode 100644 app/views/entities/_form.html.erb create mode 100644 app/views/entities/edit.html.erb create mode 100644 app/views/entities/index.html.erb create mode 100644 app/views/entities/index.json.jbuilder create mode 100644 app/views/entities/new.html.erb create mode 100644 app/views/entities/show.html.erb create mode 100644 app/views/entities/show.json.jbuilder create mode 100644 db/migrate/20231017201238_create_entities.rb create mode 100644 test/controllers/entities_controller_test.rb create mode 100644 test/fixtures/entities.yml create mode 100644 test/models/entity_test.rb create mode 100644 test/system/entities_test.rb diff --git a/app/controllers/entities_controller.rb b/app/controllers/entities_controller.rb new file mode 100644 index 0000000..c40c262 --- /dev/null +++ b/app/controllers/entities_controller.rb @@ -0,0 +1,70 @@ +class EntitiesController < ApplicationController + before_action :set_entity, only: %i[ show edit update destroy ] + + # GET /entities or /entities.json + def index + @entities = Entity.all + end + + # GET /entities/1 or /entities/1.json + def show + end + + # GET /entities/new + def new + @entity = Entity.new + end + + # GET /entities/1/edit + def edit + end + + # POST /entities or /entities.json + def create + @entity = Entity.new(entity_params) + + respond_to do |format| + if @entity.save + format.html { redirect_to entity_url(@entity), notice: "Entity was successfully created." } + format.json { render :show, status: :created, location: @entity } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @entity.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /entities/1 or /entities/1.json + def update + respond_to do |format| + if @entity.update(entity_params) + format.html { redirect_to entity_url(@entity), notice: "Entity was successfully updated." } + format.json { render :show, status: :ok, location: @entity } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @entity.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /entities/1 or /entities/1.json + def destroy + @entity.destroy + + respond_to do |format| + format.html { redirect_to entities_url, notice: "Entity was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_entity + @entity = Entity.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def entity_params + params.require(:entity).permit(:name, :amount) + end +end diff --git a/app/helpers/entities_helper.rb b/app/helpers/entities_helper.rb new file mode 100644 index 0000000..7437498 --- /dev/null +++ b/app/helpers/entities_helper.rb @@ -0,0 +1,2 @@ +module EntitiesHelper +end diff --git a/app/models/entity.rb b/app/models/entity.rb new file mode 100644 index 0000000..5841679 --- /dev/null +++ b/app/models/entity.rb @@ -0,0 +1,2 @@ +class Entity < ApplicationRecord +end diff --git a/app/models/user.rb b/app/models/user.rb index 4756799..dcb5f12 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,4 +3,6 @@ class User < ApplicationRecord # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable + + validates :name, presence: true end diff --git a/app/views/entities/_entity.html.erb b/app/views/entities/_entity.html.erb new file mode 100644 index 0000000..49cb0de --- /dev/null +++ b/app/views/entities/_entity.html.erb @@ -0,0 +1,12 @@ +
+

+ Name: + <%= entity.name %> +

+ +

+ Amount: + <%= entity.amount %> +

+ +
diff --git a/app/views/entities/_entity.json.jbuilder b/app/views/entities/_entity.json.jbuilder new file mode 100644 index 0000000..f822f3c --- /dev/null +++ b/app/views/entities/_entity.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! entity, :id, :name, :amount, :created_at, :updated_at +json.url entity_url(entity, format: :json) diff --git a/app/views/entities/_form.html.erb b/app/views/entities/_form.html.erb new file mode 100644 index 0000000..ece8a92 --- /dev/null +++ b/app/views/entities/_form.html.erb @@ -0,0 +1,27 @@ +<%= form_with(model: entity) do |form| %> + <% if entity.errors.any? %> +
+

<%= pluralize(entity.errors.count, "error") %> prohibited this entity from being saved:

+ +
    + <% entity.errors.each do |error| %> +
  • <%= error.full_message %>
  • + <% end %> +
+
+ <% end %> + +
+ <%= form.label :name, style: "display: block" %> + <%= form.text_field :name %> +
+ +
+ <%= form.label :amount, style: "display: block" %> + <%= form.text_field :amount %> +
+ +
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/entities/edit.html.erb b/app/views/entities/edit.html.erb new file mode 100644 index 0000000..6d838ce --- /dev/null +++ b/app/views/entities/edit.html.erb @@ -0,0 +1,10 @@ +

Editing entity

+ +<%= render "form", entity: @entity %> + +
+ +
+ <%= link_to "Show this entity", @entity %> | + <%= link_to "Back to entities", entities_path %> +
diff --git a/app/views/entities/index.html.erb b/app/views/entities/index.html.erb new file mode 100644 index 0000000..a8a3051 --- /dev/null +++ b/app/views/entities/index.html.erb @@ -0,0 +1,14 @@ +

<%= notice %>

+ +

Entities

+ +
+ <% @entities.each do |entity| %> + <%= render entity %> +

+ <%= link_to "Show this entity", entity %> +

+ <% end %> +
+ +<%= link_to "New entity", new_entity_path %> diff --git a/app/views/entities/index.json.jbuilder b/app/views/entities/index.json.jbuilder new file mode 100644 index 0000000..955ceb3 --- /dev/null +++ b/app/views/entities/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @entities, partial: "entities/entity", as: :entity diff --git a/app/views/entities/new.html.erb b/app/views/entities/new.html.erb new file mode 100644 index 0000000..f7c1cdf --- /dev/null +++ b/app/views/entities/new.html.erb @@ -0,0 +1,9 @@ +

New entity

+ +<%= render "form", entity: @entity %> + +
+ +
+ <%= link_to "Back to entities", entities_path %> +
diff --git a/app/views/entities/show.html.erb b/app/views/entities/show.html.erb new file mode 100644 index 0000000..e1405a5 --- /dev/null +++ b/app/views/entities/show.html.erb @@ -0,0 +1,10 @@ +

<%= notice %>

+ +<%= render @entity %> + +
+ <%= link_to "Edit this entity", edit_entity_path(@entity) %> | + <%= link_to "Back to entities", entities_path %> + + <%= button_to "Destroy this entity", @entity, method: :delete %> +
diff --git a/app/views/entities/show.json.jbuilder b/app/views/entities/show.json.jbuilder new file mode 100644 index 0000000..0bb9794 --- /dev/null +++ b/app/views/entities/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "entities/entity", entity: @entity diff --git a/config/routes.rb b/config/routes.rb index 3fbe355..c0df4dd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + resources :entities devise_for :users resources :users diff --git a/db/migrate/20231017201238_create_entities.rb b/db/migrate/20231017201238_create_entities.rb new file mode 100644 index 0000000..2b9e8fd --- /dev/null +++ b/db/migrate/20231017201238_create_entities.rb @@ -0,0 +1,10 @@ +class CreateEntities < ActiveRecord::Migration[7.0] + def change + create_table :entities do |t| + t.string :name + t.float :amount + + t.timestamps + end + end +end diff --git a/test/controllers/entities_controller_test.rb b/test/controllers/entities_controller_test.rb new file mode 100644 index 0000000..d1f0592 --- /dev/null +++ b/test/controllers/entities_controller_test.rb @@ -0,0 +1,48 @@ +require "test_helper" + +class EntitiesControllerTest < ActionDispatch::IntegrationTest + setup do + @entity = entities(:one) + end + + test "should get index" do + get entities_url + assert_response :success + end + + test "should get new" do + get new_entity_url + assert_response :success + end + + test "should create entity" do + assert_difference("Entity.count") do + post entities_url, params: { entity: { amount: @entity.amount, name: @entity.name } } + end + + assert_redirected_to entity_url(Entity.last) + end + + test "should show entity" do + get entity_url(@entity) + assert_response :success + end + + test "should get edit" do + get edit_entity_url(@entity) + assert_response :success + end + + test "should update entity" do + patch entity_url(@entity), params: { entity: { amount: @entity.amount, name: @entity.name } } + assert_redirected_to entity_url(@entity) + end + + test "should destroy entity" do + assert_difference("Entity.count", -1) do + delete entity_url(@entity) + end + + assert_redirected_to entities_url + end +end diff --git a/test/fixtures/entities.yml b/test/fixtures/entities.yml new file mode 100644 index 0000000..878f2ee --- /dev/null +++ b/test/fixtures/entities.yml @@ -0,0 +1,9 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + name: MyString + amount: 1.5 + +two: + name: MyString + amount: 1.5 diff --git a/test/models/entity_test.rb b/test/models/entity_test.rb new file mode 100644 index 0000000..4c61596 --- /dev/null +++ b/test/models/entity_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class EntityTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/system/entities_test.rb b/test/system/entities_test.rb new file mode 100644 index 0000000..3d1df50 --- /dev/null +++ b/test/system/entities_test.rb @@ -0,0 +1,43 @@ +require "application_system_test_case" + +class EntitiesTest < ApplicationSystemTestCase + setup do + @entity = entities(:one) + end + + test "visiting the index" do + visit entities_url + assert_selector "h1", text: "Entities" + end + + test "should create entity" do + visit entities_url + click_on "New entity" + + fill_in "Amount", with: @entity.amount + fill_in "Name", with: @entity.name + click_on "Create Entity" + + assert_text "Entity was successfully created" + click_on "Back" + end + + test "should update Entity" do + visit entity_url(@entity) + click_on "Edit this entity", match: :first + + fill_in "Amount", with: @entity.amount + fill_in "Name", with: @entity.name + click_on "Update Entity" + + assert_text "Entity was successfully updated" + click_on "Back" + end + + test "should destroy Entity" do + visit entity_url(@entity) + click_on "Destroy this entity", match: :first + + assert_text "Entity was successfully destroyed" + end +end From 3550702ee0a6a9fad4c119eaa9a4484d0d2ce9c9 Mon Sep 17 00:00:00 2001 From: hassanShakur Date: Tue, 17 Oct 2023 23:13:43 +0300 Subject: [PATCH 04/39] generate Group model --- app/controllers/groups_controller.rb | 70 ++++++++++++++++++++++ app/helpers/groups_helper.rb | 2 + app/models/group.rb | 2 + app/views/groups/_form.html.erb | 27 +++++++++ app/views/groups/_group.html.erb | 12 ++++ app/views/groups/_group.json.jbuilder | 2 + app/views/groups/edit.html.erb | 10 ++++ app/views/groups/index.html.erb | 14 +++++ app/views/groups/index.json.jbuilder | 1 + app/views/groups/new.html.erb | 9 +++ app/views/groups/show.html.erb | 10 ++++ app/views/groups/show.json.jbuilder | 1 + config/routes.rb | 1 + db/migrate/20231017201330_create_groups.rb | 10 ++++ test/controllers/groups_controller_test.rb | 48 +++++++++++++++ test/fixtures/groups.yml | 9 +++ test/models/group_test.rb | 7 +++ test/system/groups_test.rb | 43 +++++++++++++ 18 files changed, 278 insertions(+) create mode 100644 app/controllers/groups_controller.rb create mode 100644 app/helpers/groups_helper.rb create mode 100644 app/models/group.rb create mode 100644 app/views/groups/_form.html.erb create mode 100644 app/views/groups/_group.html.erb create mode 100644 app/views/groups/_group.json.jbuilder create mode 100644 app/views/groups/edit.html.erb create mode 100644 app/views/groups/index.html.erb create mode 100644 app/views/groups/index.json.jbuilder create mode 100644 app/views/groups/new.html.erb create mode 100644 app/views/groups/show.html.erb create mode 100644 app/views/groups/show.json.jbuilder create mode 100644 db/migrate/20231017201330_create_groups.rb create mode 100644 test/controllers/groups_controller_test.rb create mode 100644 test/fixtures/groups.yml create mode 100644 test/models/group_test.rb create mode 100644 test/system/groups_test.rb diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb new file mode 100644 index 0000000..be229df --- /dev/null +++ b/app/controllers/groups_controller.rb @@ -0,0 +1,70 @@ +class GroupsController < ApplicationController + before_action :set_group, only: %i[ show edit update destroy ] + + # GET /groups or /groups.json + def index + @groups = Group.all + end + + # GET /groups/1 or /groups/1.json + def show + end + + # GET /groups/new + def new + @group = Group.new + end + + # GET /groups/1/edit + def edit + end + + # POST /groups or /groups.json + def create + @group = Group.new(group_params) + + respond_to do |format| + if @group.save + format.html { redirect_to group_url(@group), notice: "Group was successfully created." } + format.json { render :show, status: :created, location: @group } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @group.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /groups/1 or /groups/1.json + def update + respond_to do |format| + if @group.update(group_params) + format.html { redirect_to group_url(@group), notice: "Group was successfully updated." } + format.json { render :show, status: :ok, location: @group } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @group.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /groups/1 or /groups/1.json + def destroy + @group.destroy + + respond_to do |format| + format.html { redirect_to groups_url, notice: "Group was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_group + @group = Group.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def group_params + params.require(:group).permit(:name, :icon) + end +end diff --git a/app/helpers/groups_helper.rb b/app/helpers/groups_helper.rb new file mode 100644 index 0000000..c091b2f --- /dev/null +++ b/app/helpers/groups_helper.rb @@ -0,0 +1,2 @@ +module GroupsHelper +end diff --git a/app/models/group.rb b/app/models/group.rb new file mode 100644 index 0000000..a8e77b5 --- /dev/null +++ b/app/models/group.rb @@ -0,0 +1,2 @@ +class Group < ApplicationRecord +end diff --git a/app/views/groups/_form.html.erb b/app/views/groups/_form.html.erb new file mode 100644 index 0000000..1915910 --- /dev/null +++ b/app/views/groups/_form.html.erb @@ -0,0 +1,27 @@ +<%= form_with(model: group) do |form| %> + <% if group.errors.any? %> +
+

<%= pluralize(group.errors.count, "error") %> prohibited this group from being saved:

+ +
    + <% group.errors.each do |error| %> +
  • <%= error.full_message %>
  • + <% end %> +
+
+ <% end %> + +
+ <%= form.label :name, style: "display: block" %> + <%= form.text_field :name %> +
+ +
+ <%= form.label :icon, style: "display: block" %> + <%= form.text_field :icon %> +
+ +
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/groups/_group.html.erb b/app/views/groups/_group.html.erb new file mode 100644 index 0000000..4a0e427 --- /dev/null +++ b/app/views/groups/_group.html.erb @@ -0,0 +1,12 @@ +
+

+ Name: + <%= group.name %> +

+ +

+ Icon: + <%= group.icon %> +

+ +
diff --git a/app/views/groups/_group.json.jbuilder b/app/views/groups/_group.json.jbuilder new file mode 100644 index 0000000..2abac69 --- /dev/null +++ b/app/views/groups/_group.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! group, :id, :name, :icon, :created_at, :updated_at +json.url group_url(group, format: :json) diff --git a/app/views/groups/edit.html.erb b/app/views/groups/edit.html.erb new file mode 100644 index 0000000..23fe74b --- /dev/null +++ b/app/views/groups/edit.html.erb @@ -0,0 +1,10 @@ +

Editing group

+ +<%= render "form", group: @group %> + +
+ +
+ <%= link_to "Show this group", @group %> | + <%= link_to "Back to groups", groups_path %> +
diff --git a/app/views/groups/index.html.erb b/app/views/groups/index.html.erb new file mode 100644 index 0000000..c76a1ce --- /dev/null +++ b/app/views/groups/index.html.erb @@ -0,0 +1,14 @@ +

<%= notice %>

+ +

Groups

+ +
+ <% @groups.each do |group| %> + <%= render group %> +

+ <%= link_to "Show this group", group %> +

+ <% end %> +
+ +<%= link_to "New group", new_group_path %> diff --git a/app/views/groups/index.json.jbuilder b/app/views/groups/index.json.jbuilder new file mode 100644 index 0000000..37cc188 --- /dev/null +++ b/app/views/groups/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @groups, partial: "groups/group", as: :group diff --git a/app/views/groups/new.html.erb b/app/views/groups/new.html.erb new file mode 100644 index 0000000..a6e1adc --- /dev/null +++ b/app/views/groups/new.html.erb @@ -0,0 +1,9 @@ +

New group

+ +<%= render "form", group: @group %> + +
+ +
+ <%= link_to "Back to groups", groups_path %> +
diff --git a/app/views/groups/show.html.erb b/app/views/groups/show.html.erb new file mode 100644 index 0000000..293dbf7 --- /dev/null +++ b/app/views/groups/show.html.erb @@ -0,0 +1,10 @@ +

<%= notice %>

+ +<%= render @group %> + +
+ <%= link_to "Edit this group", edit_group_path(@group) %> | + <%= link_to "Back to groups", groups_path %> + + <%= button_to "Destroy this group", @group, method: :delete %> +
diff --git a/app/views/groups/show.json.jbuilder b/app/views/groups/show.json.jbuilder new file mode 100644 index 0000000..76d7638 --- /dev/null +++ b/app/views/groups/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "groups/group", group: @group diff --git a/config/routes.rb b/config/routes.rb index c0df4dd..f3aceda 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + resources :groups resources :entities devise_for :users resources :users diff --git a/db/migrate/20231017201330_create_groups.rb b/db/migrate/20231017201330_create_groups.rb new file mode 100644 index 0000000..e3d80b8 --- /dev/null +++ b/db/migrate/20231017201330_create_groups.rb @@ -0,0 +1,10 @@ +class CreateGroups < ActiveRecord::Migration[7.0] + def change + create_table :groups do |t| + t.string :name + t.string :icon + + t.timestamps + end + end +end diff --git a/test/controllers/groups_controller_test.rb b/test/controllers/groups_controller_test.rb new file mode 100644 index 0000000..823add0 --- /dev/null +++ b/test/controllers/groups_controller_test.rb @@ -0,0 +1,48 @@ +require "test_helper" + +class GroupsControllerTest < ActionDispatch::IntegrationTest + setup do + @group = groups(:one) + end + + test "should get index" do + get groups_url + assert_response :success + end + + test "should get new" do + get new_group_url + assert_response :success + end + + test "should create group" do + assert_difference("Group.count") do + post groups_url, params: { group: { icon: @group.icon, name: @group.name } } + end + + assert_redirected_to group_url(Group.last) + end + + test "should show group" do + get group_url(@group) + assert_response :success + end + + test "should get edit" do + get edit_group_url(@group) + assert_response :success + end + + test "should update group" do + patch group_url(@group), params: { group: { icon: @group.icon, name: @group.name } } + assert_redirected_to group_url(@group) + end + + test "should destroy group" do + assert_difference("Group.count", -1) do + delete group_url(@group) + end + + assert_redirected_to groups_url + end +end diff --git a/test/fixtures/groups.yml b/test/fixtures/groups.yml new file mode 100644 index 0000000..011e349 --- /dev/null +++ b/test/fixtures/groups.yml @@ -0,0 +1,9 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + name: MyString + icon: MyString + +two: + name: MyString + icon: MyString diff --git a/test/models/group_test.rb b/test/models/group_test.rb new file mode 100644 index 0000000..eddbcc8 --- /dev/null +++ b/test/models/group_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class GroupTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/system/groups_test.rb b/test/system/groups_test.rb new file mode 100644 index 0000000..db8864d --- /dev/null +++ b/test/system/groups_test.rb @@ -0,0 +1,43 @@ +require "application_system_test_case" + +class GroupsTest < ApplicationSystemTestCase + setup do + @group = groups(:one) + end + + test "visiting the index" do + visit groups_url + assert_selector "h1", text: "Groups" + end + + test "should create group" do + visit groups_url + click_on "New group" + + fill_in "Icon", with: @group.icon + fill_in "Name", with: @group.name + click_on "Create Group" + + assert_text "Group was successfully created" + click_on "Back" + end + + test "should update Group" do + visit group_url(@group) + click_on "Edit this group", match: :first + + fill_in "Icon", with: @group.icon + fill_in "Name", with: @group.name + click_on "Update Group" + + assert_text "Group was successfully updated" + click_on "Back" + end + + test "should destroy Group" do + visit group_url(@group) + click_on "Destroy this group", match: :first + + assert_text "Group was successfully destroyed" + end +end From 22352aaca9bb19d75fa89059c28162fe2b2f5927 Mon Sep 17 00:00:00 2001 From: hassanShakur Date: Tue, 17 Oct 2023 23:34:19 +0300 Subject: [PATCH 05/39] add model relationships --- app/models/entity.rb | 2 + app/models/group.rb | 3 ++ app/models/user.rb | 2 + config/database.yml | 4 ++ .../20231017202555_add_author_id_to_entity.rb | 6 +++ db/schema.rb | 47 +++++++++++++++++++ 6 files changed, 64 insertions(+) create mode 100644 db/migrate/20231017202555_add_author_id_to_entity.rb create mode 100644 db/schema.rb diff --git a/app/models/entity.rb b/app/models/entity.rb index 5841679..bd5749c 100644 --- a/app/models/entity.rb +++ b/app/models/entity.rb @@ -1,2 +1,4 @@ class Entity < ApplicationRecord + validates :name, presence: true + has_and_belongs_to_many :groups end diff --git a/app/models/group.rb b/app/models/group.rb index a8e77b5..04b478f 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -1,2 +1,5 @@ class Group < ApplicationRecord + validates :name, presence: true + belongs_to :user + has_and_belongs_to_many :entities end diff --git a/app/models/user.rb b/app/models/user.rb index dcb5f12..0c8328b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,4 +5,6 @@ class User < ApplicationRecord :recoverable, :rememberable, :validatable validates :name, presence: true + has_many :entities, dependent: :destroy + has_many :groups, dependent: :destroy end diff --git a/config/database.yml b/config/database.yml index 32624f6..7c83c7c 100644 --- a/config/database.yml +++ b/config/database.yml @@ -24,6 +24,8 @@ default: &default development: <<: *default database: budget_app_development + username: Dev + password: 12345678 # The specified database role being used to connect to postgres. # To create additional roles in postgres see `$ createuser --help`. @@ -58,6 +60,8 @@ development: test: <<: *default database: budget_app_test + username: Dev + password: 12345678 # As with config/credentials.yml, you never want to store sensitive information, # like your database password, in your source code. If your source code is diff --git a/db/migrate/20231017202555_add_author_id_to_entity.rb b/db/migrate/20231017202555_add_author_id_to_entity.rb new file mode 100644 index 0000000..2516425 --- /dev/null +++ b/db/migrate/20231017202555_add_author_id_to_entity.rb @@ -0,0 +1,6 @@ +class AddAuthorIdToEntity < ActiveRecord::Migration[7.0] + def change + # Add column author_id to table entities referencing to table users + add_reference :entities, :author, foreign_key: { to_table: :users } + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..2279b02 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,47 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema[7.0].define(version: 2023_10_17_202555) do + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + + create_table "entities", force: :cascade do |t| + t.string "name" + t.float "amount" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.bigint "author_id" + t.index ["author_id"], name: "index_entities_on_author_id" + end + + create_table "groups", force: :cascade do |t| + t.string "name" + t.string "icon" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "users", force: :cascade do |t| + t.string "name" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "email", default: "", null: false + t.string "encrypted_password", default: "", null: false + t.string "reset_password_token" + t.datetime "reset_password_sent_at" + t.datetime "remember_created_at" + t.index ["email"], name: "index_users_on_email", unique: true + t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true + end + + add_foreign_key "entities", "users", column: "author_id" +end From a020052e337f8419d045484a2da3669dcb5dfa12 Mon Sep 17 00:00:00 2001 From: hassanShakur Date: Tue, 17 Oct 2023 23:54:45 +0300 Subject: [PATCH 06/39] add splash screen setup --- app/controllers/application_controller.rb | 19 +++++++++++++++++++ app/controllers/entities_controller.rb | 1 + app/controllers/groups_controller.rb | 1 + app/controllers/users_controller.rb | 1 + app/views/users/splash.html.erb | 6 ++++++ app/views/welcome/index.html.erb | 7 +++++++ config/routes.rb | 2 +- 7 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 app/views/users/splash.html.erb create mode 100644 app/views/welcome/index.html.erb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 09705d1..8a47aa5 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,2 +1,21 @@ class ApplicationController < ActionController::Base + protect_from_forgery with: :exception + before_action :update_allowed_parameters, if: :devise_controller? + + def after_sign_in_path_for(_resource) + users_path + end + + def after_sign_out_path_for(_resource) + root_path + end + + protected + + def update_allowed_parameters + devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(:name, :email, :password) } + devise_parameter_sanitizer.permit(:account_update) do |u| + u.permit(:name, :email, :password, :current_password) + end + end end diff --git a/app/controllers/entities_controller.rb b/app/controllers/entities_controller.rb index c40c262..dda2e01 100644 --- a/app/controllers/entities_controller.rb +++ b/app/controllers/entities_controller.rb @@ -1,4 +1,5 @@ class EntitiesController < ApplicationController + before_action :authenticate_user! before_action :set_entity, only: %i[ show edit update destroy ] # GET /entities or /entities.json diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index be229df..6b0edf6 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -1,4 +1,5 @@ class GroupsController < ApplicationController + before_action :authenticate_user! before_action :set_group, only: %i[ show edit update destroy ] # GET /groups or /groups.json diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 279094d..2a25d5b 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,4 +1,5 @@ class UsersController < ApplicationController + before_action :authenticate_user! before_action :set_user, only: %i[ show edit update destroy ] # GET /users or /users.json diff --git a/app/views/users/splash.html.erb b/app/views/users/splash.html.erb new file mode 100644 index 0000000..08445ca --- /dev/null +++ b/app/views/users/splash.html.erb @@ -0,0 +1,6 @@ +
+

My App

+ + <%= link_to "Login", new_user_session_path %> + <%= link_to "Sign Up", new_user_registration_path %> +
\ No newline at end of file diff --git a/app/views/welcome/index.html.erb b/app/views/welcome/index.html.erb new file mode 100644 index 0000000..c62b4d1 --- /dev/null +++ b/app/views/welcome/index.html.erb @@ -0,0 +1,7 @@ +
+ +

My App

+ + <%= link_to "Login", login_path %> + <%= link_to "Sign Up", signup_path %> +
diff --git a/config/routes.rb b/config/routes.rb index f3aceda..6b35eab 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,7 +4,7 @@ devise_for :users resources :users - root 'users#index' + root 'users#splash' # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Defines the root path route ("/") From db2fa66eec15db699407c3bad060f9dde04305bb Mon Sep 17 00:00:00 2001 From: hassanShakur Date: Wed, 18 Oct 2023 08:07:18 +0300 Subject: [PATCH 07/39] design splash screen --- app/assets/stylesheets/application.css | 76 ++++++++++++++++++++ app/controllers/users_controller.rb | 1 - app/views/devise/registrations/edit.html.erb | 7 +- app/views/devise/registrations/new.html.erb | 9 ++- app/views/devise/sessions/new.html.erb | 2 +- app/views/layouts/application.html.erb | 14 ++-- app/views/users/splash.html.erb | 10 +-- app/views/welcome/index.html.erb | 7 -- 8 files changed, 105 insertions(+), 21 deletions(-) delete mode 100644 app/views/welcome/index.html.erb diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 288b9ab..06d4bf0 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -13,3 +13,79 @@ *= require_tree . *= require_self */ + +* { + margin: 0; + padding: 0; + box-sizing: border-box; + transition: all 0.3s ease-in-out; +} + +a { + color: inherit; + text-decoration: none; +} + +body { + background-color: #2b2b2b; +} + +main { + max-width: 500px; + margin: 0 auto; + padding: 0; + font-family: 'Roboto', sans-serif; + font-size: 16px; + color: #434b54; + min-height: 740px; + margin-top: 2vh; + border-radius: 5px; + background-color: #fff; +} + +#splash-screen { + width: 100%; + height: 80vh; + display: grid; + grid-template-rows: 3fr 1fr; +} + +#splash-screen h1 { + display: grid; + place-items: center; +} + +#splash-links { + display: grid; + gap: 1rem; + padding: 10%; +} +/* #5fb523 #434b54 */ + +.btn { + display: grid; + place-items: center; + border-radius: 3px; +} + +.btn.pri { + background-color: #3778c2; + color: #fff; +} + +.btn.pri:hover { + background-color: #5b8cc4; +} + +.btn.sec { + background-color: transparent; + color: #434b54; +} + +.btn.sec:hover { + background-color: #ededed; +} + +#splash-links a { + height: 3rem; +} diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 2a25d5b..279094d 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,5 +1,4 @@ class UsersController < ApplicationController - before_action :authenticate_user! before_action :set_user, only: %i[ show edit update destroy ] # GET /users or /users.json diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/edit.html.erb index b82e336..7fce01c 100644 --- a/app/views/devise/registrations/edit.html.erb +++ b/app/views/devise/registrations/edit.html.erb @@ -3,9 +3,14 @@ <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %> <%= render "devise/shared/error_messages", resource: resource %> +
+ <%= f.label :name %>
+ <%= f.text_field :name, autofocus: true, autocomplete: "name" %> +
+
<%= f.label :email %>
- <%= f.email_field :email, autofocus: true, autocomplete: "email" %> + <%= f.email_field :email, autocomplete: "email" %>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %> diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb index d655b66..5c38d1d 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -3,9 +3,14 @@ <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> <%= render "devise/shared/error_messages", resource: resource %> +
+ <%= f.label :name %>
+ <%= f.text_field :name, autofocus: true, autocomplete: "name" %> +
+
<%= f.label :email %>
- <%= f.email_field :email, autofocus: true, autocomplete: "email" %> + <%= f.email_field :email, autocomplete: "email" %>
@@ -22,7 +27,7 @@
- <%= f.submit "Sign up" %> + <%= f.submit "Sign up", class: 'pri btn' %>
<% end %> diff --git a/app/views/devise/sessions/new.html.erb b/app/views/devise/sessions/new.html.erb index 5ede964..d3922df 100644 --- a/app/views/devise/sessions/new.html.erb +++ b/app/views/devise/sessions/new.html.erb @@ -19,7 +19,7 @@ <% end %>
- <%= f.submit "Log in" %> + <%= f.submit "Log in", class: 'pri btn' %>
<% end %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 0d9af08..bcea72f 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,7 +1,7 @@ - BudgetApp + Budgeter <%= csrf_meta_tags %> <%= csp_meta_tag %> @@ -11,9 +11,13 @@ -

<%= notice %>

-

<%= alert %>

- - <%= yield %> +
+ <% if user_signed_in? then %> + + <% end %> + <%= yield %> +
diff --git a/app/views/users/splash.html.erb b/app/views/users/splash.html.erb index 08445ca..d5d9bbd 100644 --- a/app/views/users/splash.html.erb +++ b/app/views/users/splash.html.erb @@ -1,6 +1,8 @@ -
-

My App

+
+

Budgeter

- <%= link_to "Login", new_user_session_path %> - <%= link_to "Sign Up", new_user_registration_path %> +
\ No newline at end of file diff --git a/app/views/welcome/index.html.erb b/app/views/welcome/index.html.erb deleted file mode 100644 index c62b4d1..0000000 --- a/app/views/welcome/index.html.erb +++ /dev/null @@ -1,7 +0,0 @@ -
- -

My App

- - <%= link_to "Login", login_path %> - <%= link_to "Sign Up", signup_path %> -
From ef957f857e8a92988df56c03ee7eb706a2e3cacf Mon Sep 17 00:00:00 2001 From: hassanShakur Date: Wed, 18 Oct 2023 08:40:56 +0300 Subject: [PATCH 08/39] add styles to general forms --- app/assets/stylesheets/application.css | 46 ++++++++++++++++++- app/views/devise/passwords/new.html.erb | 25 ++++++----- app/views/devise/registrations/new.html.erb | 49 +++++++++------------ app/views/devise/sessions/new.html.erb | 34 ++++++-------- app/views/devise/shared/_links.html.erb | 4 +- 5 files changed, 95 insertions(+), 63 deletions(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 06d4bf0..d711680 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -66,6 +66,8 @@ main { display: grid; place-items: center; border-radius: 3px; + padding: 0.8rem; + margin-top: 10px; } .btn.pri { @@ -79,7 +81,7 @@ main { .btn.sec { background-color: transparent; - color: #434b54; + color: #434b54 !important; } .btn.sec:hover { @@ -89,3 +91,45 @@ main { #splash-links a { height: 3rem; } + +#form { + display: grid; + padding: 1rem 4%; +} + +#form > a { + text-align: center; + margin-top: 10px; + color: #434b546a; +} + +#form h2 { + text-align: center; + padding: 1rem; + margin-bottom: 2rem; +} + +#form form { + display: grid; + gap: 1rem; +} + +form > div { + display: grid; +} + +input, +button { + outline: none; + border: none; + padding: 0.8rem 0rem; +} + +.field input { + border-bottom: 1px solid #2b2b2b76; + font-size: 14px; +} + +.actions { + margin-top: 2rem; +} diff --git a/app/views/devise/passwords/new.html.erb b/app/views/devise/passwords/new.html.erb index 9b486b8..0f61a3c 100644 --- a/app/views/devise/passwords/new.html.erb +++ b/app/views/devise/passwords/new.html.erb @@ -1,16 +1,17 @@ -

Forgot your password?

+
+

Forgot your password?

-<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :post }) do |f| %> - <%= render "devise/shared/error_messages", resource: resource %> + <%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :post }) do |f| %> + <%= render "devise/shared/error_messages", resource: resource %> -
- <%= f.label :email %>
- <%= f.email_field :email, autofocus: true, autocomplete: "email" %> -
+
+ <%= f.email_field :email, autofocus: true, autocomplete: "email", placeholder: 'Email' %> +
-
- <%= f.submit "Send me reset password instructions" %> -
-<% end %> +
+ <%= f.submit "Send me reset password instructions", class: 'pri btn' %> +
+ <% end %> -<%= render "devise/shared/links" %> + <%= render "devise/shared/links" %> +
diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb index 5c38d1d..4873aea 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -1,34 +1,29 @@ -

Sign up

+
+

Sign up

-<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> - <%= render "devise/shared/error_messages", resource: resource %> + <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> + <%= render "devise/shared/error_messages", resource: resource %> -
- <%= f.label :name %>
- <%= f.text_field :name, autofocus: true, autocomplete: "name" %> -
+
+ <%= f.text_field :name, autofocus: true, autocomplete: "name", placeholder: 'Name' %> +
-
- <%= f.label :email %>
- <%= f.email_field :email, autocomplete: "email" %> -
+
+ <%= f.email_field :email, autocomplete: "email", placeholder: 'Email' %> +
-
- <%= f.label :password %> - <% if @minimum_password_length %> - (<%= @minimum_password_length %> characters minimum) - <% end %>
- <%= f.password_field :password, autocomplete: "new-password" %> -
+
+ <%= f.password_field :password, autocomplete: "new-password", placeholder: 'Password (6 characters minimum)' %> +
-
- <%= f.label :password_confirmation %>
- <%= f.password_field :password_confirmation, autocomplete: "new-password" %> -
+
+ <%= f.password_field :password_confirmation, autocomplete: "new-password", placeholder: 'Confirm Password' %> +
-
- <%= f.submit "Sign up", class: 'pri btn' %> -
-<% end %> +
+ <%= f.submit "Sign up", class: 'pri btn' %> +
+ <% end %> -<%= render "devise/shared/links" %> + <%= render "devise/shared/links" %> +
diff --git a/app/views/devise/sessions/new.html.erb b/app/views/devise/sessions/new.html.erb index d3922df..2c1225f 100644 --- a/app/views/devise/sessions/new.html.erb +++ b/app/views/devise/sessions/new.html.erb @@ -1,26 +1,18 @@ -

Log in

- -<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> -
- <%= f.label :email %>
- <%= f.email_field :email, autofocus: true, autocomplete: "email" %> -
- -
- <%= f.label :password %>
- <%= f.password_field :password, autocomplete: "current-password" %> -
+
+

Log in

+ <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> +
+ <%= f.email_field :email, autofocus: true, autocomplete: "email", placeholder: 'Email' %> +
- <% if devise_mapping.rememberable? %>
- <%= f.check_box :remember_me %> - <%= f.label :remember_me %> + <%= f.password_field :password, autocomplete: "current-password", placeholder: 'Password' %>
- <% end %> -
- <%= f.submit "Log in", class: 'pri btn' %> -
-<% end %> +
+ <%= f.submit "Log in", class: 'pri btn' %> +
+ <% end %> -<%= render "devise/shared/links" %> + <%= render "devise/shared/links" %> +
\ No newline at end of file diff --git a/app/views/devise/shared/_links.html.erb b/app/views/devise/shared/_links.html.erb index 7a75304..39fafb6 100644 --- a/app/views/devise/shared/_links.html.erb +++ b/app/views/devise/shared/_links.html.erb @@ -1,9 +1,9 @@ <%- if controller_name != 'sessions' %> - <%= link_to "Log in", new_session_path(resource_name) %>
+ <%= link_to "Log in", new_session_path(resource_name), class: 'sec btn' %>
<% end %> <%- if devise_mapping.registerable? && controller_name != 'registrations' %> - <%= link_to "Sign up", new_registration_path(resource_name) %>
+ <%= link_to "Sign up", new_registration_path(resource_name), class: 'sec btn' %>
<% end %> <%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %> From 2ce342b8f4082b0acf35e8885fd6c149c48531bc Mon Sep 17 00:00:00 2001 From: hassanShakur Date: Wed, 18 Oct 2023 11:51:55 +0300 Subject: [PATCH 09/39] initial category index design --- app/assets/stylesheets/application.css | 48 +++++++++++- app/controllers/application_controller.rb | 4 +- app/controllers/groups_controller.rb | 3 +- app/controllers/users_controller.rb | 13 ---- app/models/group.rb | 1 + app/models/user.rb | 1 - app/views/devise/registrations/edit.html.erb | 78 +++++++++---------- app/views/devise/registrations/new.html.erb | 2 +- app/views/groups/_form.html.erb | 10 +-- app/views/groups/_group.html.erb | 17 ++-- app/views/groups/index.html.erb | 12 +-- app/views/groups/new.html.erb | 12 +-- app/views/layouts/application.html.erb | 1 + app/views/users/splash.html.erb | 2 +- config/routes.rb | 10 ++- .../20231018062517_add_user_id_to_group.rb | 6 ++ ...83822_create_groups_entities_join_table.rb | 8 ++ db/schema.rb | 11 ++- 18 files changed, 148 insertions(+), 91 deletions(-) create mode 100644 db/migrate/20231018062517_add_user_id_to_group.rb create mode 100644 db/migrate/20231018083822_create_groups_entities_join_table.rb diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index d711680..bcb6165 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -70,9 +70,11 @@ main { margin-top: 10px; } -.btn.pri { +.btn.pri, input[type='submit'] { background-color: #3778c2; color: #fff; + padding: 0.8rem; + margin: 10px 0; } .btn.pri:hover { @@ -88,6 +90,15 @@ main { background-color: #ededed; } +.btn.action { + background-color: #5fb523; + color: #fff; +} + +.btn.action:hover { + background-color: #78b74b; +} + #splash-links a { height: 3rem; } @@ -97,12 +108,16 @@ main { padding: 1rem 4%; } -#form > a { +#form > a, #back { text-align: center; margin-top: 10px; color: #434b546a; } +#back { + margin-top: 20px; +} + #form h2 { text-align: center; padding: 1rem; @@ -133,3 +148,32 @@ button { .actions { margin-top: 2rem; } + +#groups { + display: grid; + gap: 1rem; + padding: 1rem; + background-color: #f5f5f5; +} + +#group { + display: grid; + grid-template-columns: 1fr 3fr 1fr; + gap: 1rem; + padding: .6rem; + background-color: #fff; + border-radius: 5px; +} + +#group .img-div { + width: 100%; + height: 80px; + overflow: hidden; + border-radius: 5px; +} + +#group img { + width: 100%; + height: 100%; + object-fit: cover; +} \ No newline at end of file diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 8a47aa5..7b2e7be 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,11 +3,11 @@ class ApplicationController < ActionController::Base before_action :update_allowed_parameters, if: :devise_controller? def after_sign_in_path_for(_resource) - users_path + authenticated_root_path end def after_sign_out_path_for(_resource) - root_path + unauthenticated_root_path end protected diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index 6b0edf6..a54dc3e 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -23,14 +23,13 @@ def edit # POST /groups or /groups.json def create @group = Group.new(group_params) + @group.user = current_user respond_to do |format| if @group.save format.html { redirect_to group_url(@group), notice: "Group was successfully created." } - format.json { render :show, status: :created, location: @group } else format.html { render :new, status: :unprocessable_entity } - format.json { render json: @group.errors, status: :unprocessable_entity } end end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 279094d..c7a40ee 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,24 +1,11 @@ class UsersController < ApplicationController before_action :set_user, only: %i[ show edit update destroy ] - # GET /users or /users.json - def index - @users = User.all - end - - # GET /users/1 or /users/1.json - def show - end - # GET /users/new def new @user = User.new end - # GET /users/1/edit - def edit - end - # POST /users or /users.json def create @user = User.new(user_params) diff --git a/app/models/group.rb b/app/models/group.rb index 04b478f..7e234fe 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -1,5 +1,6 @@ class Group < ApplicationRecord validates :name, presence: true + validates :icon, presence: true belongs_to :user has_and_belongs_to_many :entities end diff --git a/app/models/user.rb b/app/models/user.rb index 0c8328b..ba67cfa 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,5 +6,4 @@ class User < ApplicationRecord validates :name, presence: true has_many :entities, dependent: :destroy - has_many :groups, dependent: :destroy end diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/edit.html.erb index 7fce01c..588f9d9 100644 --- a/app/views/devise/registrations/edit.html.erb +++ b/app/views/devise/registrations/edit.html.erb @@ -1,48 +1,48 @@ -

Edit <%= resource_name.to_s.humanize %>

+
+

Edit <%= resource_name.to_s.humanize %>

-<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %> - <%= render "devise/shared/error_messages", resource: resource %> + <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %> + <%= render "devise/shared/error_messages", resource: resource %> -
- <%= f.label :name %>
- <%= f.text_field :name, autofocus: true, autocomplete: "name" %> -
+
+ <%= f.text_field :name, autofocus: true, autocomplete: "name", placeholder: 'Full Name' %> +
-
- <%= f.label :email %>
- <%= f.email_field :email, autocomplete: "email" %> -
+
+ <%= f.email_field :email, autocomplete: "email", placeholder: 'Email' %> +
- <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %> -
Currently waiting confirmation for: <%= resource.unconfirmed_email %>
- <% end %> - -
- <%= f.label :password %> (leave blank if you don't want to change it)
- <%= f.password_field :password, autocomplete: "new-password" %> - <% if @minimum_password_length %> -
- <%= @minimum_password_length %> characters minimum + <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %> +
Currently waiting confirmation for: <%= resource.unconfirmed_email %>
<% end %> -
- -
- <%= f.label :password_confirmation %>
- <%= f.password_field :password_confirmation, autocomplete: "new-password" %> -
-
- <%= f.label :current_password %> (we need your current password to confirm your changes)
- <%= f.password_field :current_password, autocomplete: "current-password" %> -
- -
- <%= f.submit "Update" %> -
-<% end %> +
+ <%= f.label :password %> (leave blank if you don't want to change it)
+ <%= f.password_field :password, autocomplete: "new-password" %> + <% if @minimum_password_length %> +
+ <%= @minimum_password_length %> characters minimum + <% end %> +
+ +
+ <%= f.label :password_confirmation %>
+ <%= f.password_field :password_confirmation, autocomplete: "new-password" %> +
+ +
+ <%= f.label :current_password %> (we need your )
+ <%= f.password_field :current_password, autocomplete: "current-password", placeholder: 'Enter current password to confirm your changes' %> +
+ +
+ <%= f.submit "Update" %> +
+ <% end %> -

Cancel my account

+

Cancel my account

-
Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?", turbo_confirm: "Are you sure?" }, method: :delete %>
+
Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?", turbo_confirm: "Are you sure?" }, method: :delete %>
-<%= link_to "Back", :back %> + <%= link_to "Back", :back %> +
diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb index 4873aea..9e8bd2a 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -5,7 +5,7 @@ <%= render "devise/shared/error_messages", resource: resource %>
- <%= f.text_field :name, autofocus: true, autocomplete: "name", placeholder: 'Name' %> + <%= f.text_field :name, autofocus: true, autocomplete: "name", placeholder: 'Full Name' %>
diff --git a/app/views/groups/_form.html.erb b/app/views/groups/_form.html.erb index 1915910..2b0dd23 100644 --- a/app/views/groups/_form.html.erb +++ b/app/views/groups/_form.html.erb @@ -11,14 +11,12 @@
<% end %> -
- <%= form.label :name, style: "display: block" %> - <%= form.text_field :name %> +
+ <%= form.text_field :name, placeholder: 'Name' %>
-
- <%= form.label :icon, style: "display: block" %> - <%= form.text_field :icon %> +
+ <%= form.text_field :icon, placeholder: 'Icon' %>
diff --git a/app/views/groups/_group.html.erb b/app/views/groups/_group.html.erb index 4a0e427..f49068e 100644 --- a/app/views/groups/_group.html.erb +++ b/app/views/groups/_group.html.erb @@ -1,12 +1,15 @@ -
-

- Name: - <%= group.name %> -

+
+
+ <%= image_tag group.icon %> +
+ +
+

<%= group.name %>

+ <%= group.created_at.strftime("%d %b %Y") %> +

- Icon: - <%= group.icon %> + $<%= group.entities.sum(:amount) %>

diff --git a/app/views/groups/index.html.erb b/app/views/groups/index.html.erb index c76a1ce..762da96 100644 --- a/app/views/groups/index.html.erb +++ b/app/views/groups/index.html.erb @@ -1,14 +1,8 @@ -

<%= notice %>

-

Groups

+

Categories

- <% @groups.each do |group| %> - <%= render group %> -

- <%= link_to "Show this group", group %> -

- <% end %> + <%= render @groups %>
-<%= link_to "New group", new_group_path %> +<%= link_to "Add new category", new_group_path, class: 'action btn' %> diff --git a/app/views/groups/new.html.erb b/app/views/groups/new.html.erb index a6e1adc..3b50ca1 100644 --- a/app/views/groups/new.html.erb +++ b/app/views/groups/new.html.erb @@ -1,9 +1,11 @@ -

New group

+
+

New group

-<%= render "form", group: @group %> + <%= render "form", group: @group %> -
+
-
- <%= link_to "Back to groups", groups_path %> +
+ <%= link_to "Back to groups", groups_path, id: 'back' %> +
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index bcea72f..4901471 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -12,6 +12,7 @@
+

<%= notice %>

<% if user_signed_in? then %>
-
\ No newline at end of file +
diff --git a/config/routes.rb b/config/routes.rb index 6b35eab..6d3c7e4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,9 +2,15 @@ resources :groups resources :entities devise_for :users - resources :users - root 'users#splash' + # make users#splash the root path only if the user is not signed in, else make groups#index the root path + authenticated :user do + root 'groups#index', as: :authenticated_root + end + + unauthenticated do + root 'users#splash', as: :unauthenticated_root + end # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Defines the root path route ("/") diff --git a/db/migrate/20231018062517_add_user_id_to_group.rb b/db/migrate/20231018062517_add_user_id_to_group.rb new file mode 100644 index 0000000..1dc8445 --- /dev/null +++ b/db/migrate/20231018062517_add_user_id_to_group.rb @@ -0,0 +1,6 @@ +class AddUserIdToGroup < ActiveRecord::Migration[7.0] + def change + add_column :groups, :user_id, :integer + add_index :groups, :user_id + end +end diff --git a/db/migrate/20231018083822_create_groups_entities_join_table.rb b/db/migrate/20231018083822_create_groups_entities_join_table.rb new file mode 100644 index 0000000..44dbbe9 --- /dev/null +++ b/db/migrate/20231018083822_create_groups_entities_join_table.rb @@ -0,0 +1,8 @@ +class CreateGroupsEntitiesJoinTable < ActiveRecord::Migration[7.0] + def change + create_join_table :groups, :entities do |t| + t.index :group_id + t.index :entity_id + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 2279b02..5d69105 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_10_17_202555) do +ActiveRecord::Schema[7.0].define(version: 2023_10_18_083822) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -23,11 +23,20 @@ t.index ["author_id"], name: "index_entities_on_author_id" end + create_table "entities_groups", id: false, force: :cascade do |t| + t.bigint "group_id", null: false + t.bigint "entity_id", null: false + t.index ["entity_id"], name: "index_entities_groups_on_entity_id" + t.index ["group_id"], name: "index_entities_groups_on_group_id" + end + create_table "groups", force: :cascade do |t| t.string "name" t.string "icon" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "user_id" + t.index ["user_id"], name: "index_groups_on_user_id" end create_table "users", force: :cascade do |t| From dd5719e75102d7c20b2e31d9b05a8a79c1215eb8 Mon Sep 17 00:00:00 2001 From: hassanShakur Date: Wed, 18 Oct 2023 17:41:26 +0300 Subject: [PATCH 10/39] create entity logic --- app/assets/stylesheets/application.css | 58 ++++++++++++++++++++++---- app/controllers/entities_controller.rb | 46 ++++---------------- app/controllers/groups_controller.rb | 1 + app/views/entities/_form.html.erb | 12 +++--- app/views/entities/new.html.erb | 12 +++--- app/views/groups/_group.html.erb | 26 ++++++------ app/views/groups/index.html.erb | 13 +++--- app/views/groups/show.html.erb | 27 +++++++++--- app/views/layouts/application.html.erb | 4 +- config/routes.rb | 8 +++- 10 files changed, 120 insertions(+), 87 deletions(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index bcb6165..81bda28 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -43,6 +43,12 @@ main { background-color: #fff; } +.header { + background-color: #3778c2; + color: #fff; + padding: 1rem 0.6rem; +} + #splash-screen { width: 100%; height: 80vh; @@ -61,16 +67,40 @@ main { padding: 10%; } /* #5fb523 #434b54 */ +#logout { + background-color: #3778c2; + display: flex; + align-items: center; + justify-content: flex-end; + padding: 0 0.5rem; + border-bottom: 1px solid #fff; +} + +#logout .btn { + background-color: red; + color: #fff; + width: fit-content; + padding: 0.7rem 2rem; + margin-bottom: 15px; +} + +#logout .btn:hover { + background-color: #fa3131; + cursor: pointer; +} .btn { display: grid; place-items: center; border-radius: 3px; padding: 0.8rem; + width: 90%; + margin: 0 auto; margin-top: 10px; } -.btn.pri, input[type='submit'] { +.btn.pri, +input[type='submit'] { background-color: #3778c2; color: #fff; padding: 0.8rem; @@ -108,10 +138,11 @@ main { padding: 1rem 4%; } -#form > a, #back { - text-align: center; - margin-top: 10px; - color: #434b546a; +#form > a, +#back { + text-align: center; + margin-top: 10px; + color: #434b546a; } #back { @@ -159,21 +190,30 @@ button { #group { display: grid; grid-template-columns: 1fr 3fr 1fr; - gap: 1rem; - padding: .6rem; + gap: 1.4rem; + padding: 0.6rem; background-color: #fff; border-radius: 5px; } +#group:hover { + background-color: #d1d1d190; +} + #group .img-div { width: 100%; height: 80px; overflow: hidden; - border-radius: 5px; + border-radius: 2px; } #group img { width: 100%; height: 100%; object-fit: cover; -} \ No newline at end of file +} + +.group-info { + display: grid; + justify-content: space-between; +} diff --git a/app/controllers/entities_controller.rb b/app/controllers/entities_controller.rb index dda2e01..d747d3f 100644 --- a/app/controllers/entities_controller.rb +++ b/app/controllers/entities_controller.rb @@ -1,23 +1,12 @@ class EntitiesController < ApplicationController before_action :authenticate_user! + before_action :set_group before_action :set_entity, only: %i[ show edit update destroy ] - # GET /entities or /entities.json - def index - @entities = Entity.all - end - - # GET /entities/1 or /entities/1.json - def show - end - # GET /entities/new def new @entity = Entity.new - end - - # GET /entities/1/edit - def edit + @group = Group.find(params[:group_id]) end # POST /entities or /entities.json @@ -26,44 +15,23 @@ def create respond_to do |format| if @entity.save - format.html { redirect_to entity_url(@entity), notice: "Entity was successfully created." } - format.json { render :show, status: :created, location: @entity } + format.html { redirect_to group_path(@group), notice: "Entity was successfully created." } else format.html { render :new, status: :unprocessable_entity } - format.json { render json: @entity.errors, status: :unprocessable_entity } end end end - # PATCH/PUT /entities/1 or /entities/1.json - def update - respond_to do |format| - if @entity.update(entity_params) - format.html { redirect_to entity_url(@entity), notice: "Entity was successfully updated." } - format.json { render :show, status: :ok, location: @entity } - else - format.html { render :edit, status: :unprocessable_entity } - format.json { render json: @entity.errors, status: :unprocessable_entity } - end - end - end - - # DELETE /entities/1 or /entities/1.json - def destroy - @entity.destroy - - respond_to do |format| - format.html { redirect_to entities_url, notice: "Entity was successfully destroyed." } - format.json { head :no_content } - end - end - private # Use callbacks to share common setup or constraints between actions. def set_entity @entity = Entity.find(params[:id]) end + def set_group + @group = Group.find(params[:group_id]) + end + # Only allow a list of trusted parameters through. def entity_params params.require(:entity).permit(:name, :amount) diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index a54dc3e..30798a1 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -9,6 +9,7 @@ def index # GET /groups/1 or /groups/1.json def show + @transactions = @group.entities end # GET /groups/new diff --git a/app/views/entities/_form.html.erb b/app/views/entities/_form.html.erb index ece8a92..824048c 100644 --- a/app/views/entities/_form.html.erb +++ b/app/views/entities/_form.html.erb @@ -1,4 +1,4 @@ -<%= form_with(model: entity) do |form| %> +<%= form_with model: entity, url: group_entities_path(@group), local: true do |form| %> <% if entity.errors.any? %>

<%= pluralize(entity.errors.count, "error") %> prohibited this entity from being saved:

@@ -11,14 +11,12 @@
<% end %> -
- <%= form.label :name, style: "display: block" %> - <%= form.text_field :name %> +
+ <%= form.text_field :name, placeholder: 'Name' %>
-
- <%= form.label :amount, style: "display: block" %> - <%= form.text_field :amount %> +
+ <%= form.text_field :amount, placeholder: 'Amount' %>
diff --git a/app/views/entities/new.html.erb b/app/views/entities/new.html.erb index f7c1cdf..6ceaf44 100644 --- a/app/views/entities/new.html.erb +++ b/app/views/entities/new.html.erb @@ -1,9 +1,11 @@ -

New entity

+
+

New entity

-<%= render "form", entity: @entity %> + <%= render "form", entity: @entity %> -
+
-
- <%= link_to "Back to entities", entities_path %> +
+ <%= link_to "Back to transactions", group_path(@group), id: 'back' %> +
diff --git a/app/views/groups/_group.html.erb b/app/views/groups/_group.html.erb index f49068e..5fdaddf 100644 --- a/app/views/groups/_group.html.erb +++ b/app/views/groups/_group.html.erb @@ -1,15 +1,17 @@ -
-
- <%= image_tag group.icon %> -
+<%= link_to group_path(group) do %> +
+
+ <%= image_tag group.icon %> +
-
-

<%= group.name %>

- <%= group.created_at.strftime("%d %b %Y") %> -
+
+

<%= group.name %>

+ <%= group.created_at.strftime("%d %b %Y") %> +
-

- $<%= group.entities.sum(:amount) %> -

+

+ $<%= group.entities.sum(:amount) %> +

-
+
+<% end %> diff --git a/app/views/groups/index.html.erb b/app/views/groups/index.html.erb index 762da96..a0229aa 100644 --- a/app/views/groups/index.html.erb +++ b/app/views/groups/index.html.erb @@ -1,8 +1,11 @@ +
+
+

Categories

+
-

Categories

+
+ <%= render @groups %> +
-
- <%= render @groups %> + <%= link_to "Add new category", new_group_path, class: 'action btn' %>
- -<%= link_to "Add new category", new_group_path, class: 'action btn' %> diff --git a/app/views/groups/show.html.erb b/app/views/groups/show.html.erb index 293dbf7..bc7abac 100644 --- a/app/views/groups/show.html.erb +++ b/app/views/groups/show.html.erb @@ -1,10 +1,25 @@ -

<%= notice %>

+
+ <%= link_to "Back", groups_path %> +
+ +
+
+ <%= image_tag @group.icon %> +
-<%= render @group %> +
+

<%= @group.name %>

+

+ Total Transaction Amount: $<%= @group.entities.sum(:amount) %> +

+
+
-
- <%= link_to "Edit this group", edit_group_path(@group) %> | - <%= link_to "Back to groups", groups_path %> +
+ <%= render partial: 'entities/entity', collection: @transactions %> - <%= button_to "Destroy this group", @group, method: :delete %> + <%= link_to "Add a new transaction", new_group_entity_path(@group), class: 'action btn' %>
+ + + diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 4901471..500a263 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -14,9 +14,9 @@

<%= notice %>

<% if user_signed_in? then %> - +
<% end %> <%= yield %> diff --git a/config/routes.rb b/config/routes.rb index 6d3c7e4..6564631 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,8 +1,12 @@ Rails.application.routes.draw do - resources :groups - resources :entities devise_for :users + resources :groups, only: %i[index show new create] do + resources :entities, only: %i[create new] + end + + get '/groups/:group_id/entities/new_form', to: 'entities#new', as: 'new_group_entity_form' + # make users#splash the root path only if the user is not signed in, else make groups#index the root path authenticated :user do root 'groups#index', as: :authenticated_root From c79f57e7e18f24aa0a3a685c37c113d2ebf2ef69 Mon Sep 17 00:00:00 2001 From: hassanShakur Date: Wed, 18 Oct 2023 17:50:48 +0300 Subject: [PATCH 11/39] fix entity create redirection --- app/assets/stylesheets/application.css | 2 +- app/controllers/entities_controller.rb | 4 +++- app/views/groups/show.html.erb | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 81bda28..d51a80e 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -94,7 +94,7 @@ main { place-items: center; border-radius: 3px; padding: 0.8rem; - width: 90%; + width: 94%; margin: 0 auto; margin-top: 10px; } diff --git a/app/controllers/entities_controller.rb b/app/controllers/entities_controller.rb index d747d3f..1c360ba 100644 --- a/app/controllers/entities_controller.rb +++ b/app/controllers/entities_controller.rb @@ -1,6 +1,6 @@ class EntitiesController < ApplicationController before_action :authenticate_user! - before_action :set_group + before_action :set_group, only: %i[ new create ] before_action :set_entity, only: %i[ show edit update destroy ] # GET /entities/new @@ -12,6 +12,8 @@ def new # POST /entities or /entities.json def create @entity = Entity.new(entity_params) + @entity.groups << @group + @entity.author_id = current_user.id respond_to do |format| if @entity.save diff --git a/app/views/groups/show.html.erb b/app/views/groups/show.html.erb index bc7abac..53475e1 100644 --- a/app/views/groups/show.html.erb +++ b/app/views/groups/show.html.erb @@ -16,7 +16,7 @@
- <%= render partial: 'entities/entity', collection: @transactions %> + <%= render partial: 'entities/entity', collection: @group.entities %> <%= link_to "Add a new transaction", new_group_entity_path(@group), class: 'action btn' %>
From 53f95f1d9604c3acbb6c8d8e7fe6b435c6c32a82 Mon Sep 17 00:00:00 2001 From: hassanShakur Date: Wed, 18 Oct 2023 18:06:13 +0300 Subject: [PATCH 12/39] finalize entity list styling --- app/assets/stylesheets/application.css | 28 ++++++++++++++++++++++++++ app/views/entities/_entity.html.erb | 17 ++++++++-------- app/views/groups/_group.html.erb | 2 +- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index d51a80e..a4c8998 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -49,6 +49,10 @@ main { padding: 1rem 0.6rem; } +.header h1 { + font-size: 1.4rem; +} + #splash-screen { width: 100%; height: 80vh; @@ -217,3 +221,27 @@ button { display: grid; justify-content: space-between; } + +#transactions { + display: grid; + gap: 1rem; + margin-top: 3rem; +} + +#entity { + display: grid; + gap: 1rem; + padding: 0.6rem; + border-bottom: 1px solid #2b2b2b76; +} + +#entity > div { + display: flex; + justify-content: space-between; + align-items: center; +} + +#date { + opacity: .5; + font-size: 12px; +} \ No newline at end of file diff --git a/app/views/entities/_entity.html.erb b/app/views/entities/_entity.html.erb index 49cb0de..96a94fe 100644 --- a/app/views/entities/_entity.html.erb +++ b/app/views/entities/_entity.html.erb @@ -1,12 +1,11 @@ -
-

- Name: - <%= entity.name %> -

+
+
+

Transaction: <%= entity.name %>

+

$<%= entity.amount %>

+
-

- Amount: - <%= entity.amount %> +

+ <%= entity.created_at.strftime("%d %b %Y") %> + at <%= entity.created_at.strftime("%I:%M %p") %>

-
diff --git a/app/views/groups/_group.html.erb b/app/views/groups/_group.html.erb index 5fdaddf..f66bb4d 100644 --- a/app/views/groups/_group.html.erb +++ b/app/views/groups/_group.html.erb @@ -6,7 +6,7 @@

<%= group.name %>

- <%= group.created_at.strftime("%d %b %Y") %> +

<%= group.created_at.strftime("%d %b %Y") %>

From 078b8809422ed885e3ba8ba2f2c9cce99acfee4b Mon Sep 17 00:00:00 2001 From: hassanShakur Date: Wed, 18 Oct 2023 21:35:39 +0300 Subject: [PATCH 13/39] setup linters & fix linter errors --- .github/workflows/linters.yml | 44 ++++++++++++++ .gitignore | 3 + .rubocop.yml | 60 +++++++++++++++++++ .stylelintrc.json | 37 ++++++++++++ Gemfile | 37 ++++++------ Gemfile.lock | 26 ++++++++ app/assets/stylesheets/application.css | 27 +++++---- app/controllers/entities_controller.rb | 29 ++++----- app/controllers/groups_controller.rb | 28 ++++----- app/controllers/users_controller.rb | 25 ++++---- app/jobs/application_job.rb | 7 --- app/mailers/application_mailer.rb | 4 +- app/models/entity.rb | 4 +- app/models/group.rb | 8 +-- app/views/entities/_entity.json.jbuilder | 2 - app/views/entities/edit.html.erb | 10 ---- app/views/entities/index.html.erb | 14 ----- app/views/entities/index.json.jbuilder | 1 - app/views/entities/show.html.erb | 10 ---- app/views/entities/show.json.jbuilder | 2 +- app/views/groups/_group.json.jbuilder | 2 - app/views/groups/edit.html.erb | 10 ---- app/views/groups/index.json.jbuilder | 2 +- app/views/groups/show.json.jbuilder | 2 +- app/views/users/_form.html.erb | 22 ------- app/views/users/_user.html.erb | 7 --- app/views/users/_user.json.jbuilder | 2 - app/views/users/edit.html.erb | 10 ---- app/views/users/index.html.erb | 14 ----- app/views/users/index.json.jbuilder | 2 +- app/views/users/new.html.erb | 9 --- app/views/users/show.html.erb | 10 ---- app/views/users/show.json.jbuilder | 2 +- config.ru | 2 +- test/application_system_test_case.rb | 2 +- .../application_cable/connection_test.rb | 2 +- test/controllers/entities_controller_test.rb | 20 +++---- test/controllers/groups_controller_test.rb | 20 +++---- test/controllers/users_controller_test.rb | 20 +++---- test/models/entity_test.rb | 2 +- test/models/group_test.rb | 2 +- test/models/user_test.rb | 2 +- test/system/entities_test.rb | 40 ++++++------- test/system/groups_test.rb | 40 ++++++------- test/system/users_test.rb | 36 +++++------ test/test_helper.rb | 6 +- 46 files changed, 355 insertions(+), 311 deletions(-) create mode 100644 .github/workflows/linters.yml create mode 100644 .rubocop.yml create mode 100644 .stylelintrc.json delete mode 100644 app/jobs/application_job.rb delete mode 100644 app/views/entities/_entity.json.jbuilder delete mode 100644 app/views/entities/edit.html.erb delete mode 100644 app/views/entities/index.html.erb delete mode 100644 app/views/entities/index.json.jbuilder delete mode 100644 app/views/entities/show.html.erb delete mode 100644 app/views/groups/_group.json.jbuilder delete mode 100644 app/views/groups/edit.html.erb delete mode 100644 app/views/users/_form.html.erb delete mode 100644 app/views/users/_user.html.erb delete mode 100644 app/views/users/_user.json.jbuilder delete mode 100644 app/views/users/edit.html.erb delete mode 100644 app/views/users/index.html.erb delete mode 100644 app/views/users/new.html.erb delete mode 100644 app/views/users/show.html.erb diff --git a/.github/workflows/linters.yml b/.github/workflows/linters.yml new file mode 100644 index 0000000..4671d69 --- /dev/null +++ b/.github/workflows/linters.yml @@ -0,0 +1,44 @@ +name: Linters + +on: pull_request + +env: + FORCE_COLOR: 1 + +jobs: + rubocop: + name: Rubocop + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-ruby@v1 + with: + ruby-version: 3.1.x + - name: Setup Rubocop + run: | + gem install --no-document rubocop -v '>= 1.0, < 2.0' # https://docs.rubocop.org/en/stable/installation/ + [ -f .rubocop.yml ] || wget https://raw.githubusercontent.com/microverseinc/linters-config/master/ror/.rubocop.yml + - name: Rubocop Report + run: rubocop --color + stylelint: + name: Stylelint + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: "18.x" + - name: Setup Stylelint + run: | + npm install --save-dev stylelint@13.x stylelint-scss@3.x stylelint-config-standard@21.x stylelint-csstree-validator@1.x + [ -f .stylelintrc.json ] || wget https://raw.githubusercontent.com/microverseinc/linters-config/master/ror/.stylelintrc.json + - name: Stylelint Report + run: npx stylelint "**/*.{css,scss}" + nodechecker: + name: node_modules checker + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v3 + - name: Check node_modules existence + run: | + if [ -d "node_modules/" ]; then echo -e "\e[1;31mThe node_modules/ folder was pushed to the repo. Please remove it from the GitHub repository and try again."; echo -e "\e[1;32mYou can set up a .gitignore file with this folder included on it to prevent this from happening in the future." && exit 1; fi \ No newline at end of file diff --git a/.gitignore b/.gitignore index e16dc71..e72bec7 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,6 @@ # Ignore master key for decrypting credentials and more. /config/master.key + +# .gitignore +node_modules/ diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..07baeb4 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,60 @@ +AllCops: + NewCops: enable + Exclude: + - "db/**/*" + - "bin/*" + - "config/**/*" + - "Guardfile" + - "Rakefile" + - "node_modules/**/*" + + DisplayCopNames: true + +Layout/LineLength: + Max: 120 +Metrics/MethodLength: + Include: + - "app/controllers/*" + - "app/models/*" + Max: 20 +Metrics/AbcSize: + Include: + - "app/controllers/*" + - "app/models/*" + Max: 50 +Metrics/ClassLength: + Max: 150 +Metrics/BlockLength: + AllowedMethods: ['describe'] + Max: 30 + +Style/Documentation: + Enabled: false +Style/ClassAndModuleChildren: + Enabled: false +Style/EachForSimpleLoop: + Enabled: false +Style/AndOr: + Enabled: false +Style/DefWithParentheses: + Enabled: false +Style/FrozenStringLiteralComment: + EnforcedStyle: never + +Layout/HashAlignment: + EnforcedColonStyle: key +Layout/ExtraSpacing: + AllowForAlignment: false +Layout/MultilineMethodCallIndentation: + Enabled: true + EnforcedStyle: indented +Lint/RaiseException: + Enabled: false +Lint/StructNewOverride: + Enabled: false +Style/HashEachMethods: + Enabled: false +Style/HashTransformKeys: + Enabled: false +Style/HashTransformValues: + Enabled: false \ No newline at end of file diff --git a/.stylelintrc.json b/.stylelintrc.json new file mode 100644 index 0000000..dd5d140 --- /dev/null +++ b/.stylelintrc.json @@ -0,0 +1,37 @@ +{ + "extends": ["stylelint-config-standard"], + "plugins": ["stylelint-scss", "stylelint-csstree-validator"], + "rules": { + "at-rule-no-unknown": [ + true, + { + "ignoreAtRules": [ + "tailwind", + "apply", + "variants", + "responsive", + "screen" + ] + } + ], + "scss/at-rule-no-unknown": [ + true, + { + "ignoreAtRules": [ + "tailwind", + "apply", + "variants", + "responsive", + "screen" + ] + } + ], + "csstree/validator": true + }, + "ignoreFiles": [ + "build/**", + "dist/**", + "**/reset*.css", + "**/bootstrap*.css" + ] +} diff --git a/Gemfile b/Gemfile index c5be691..1ef336d 100644 --- a/Gemfile +++ b/Gemfile @@ -1,31 +1,31 @@ -source "https://rubygems.org" +source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } -ruby "3.2.2" +ruby '3.2.2' # Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" -gem "rails", "~> 7.0.8" +gem 'rails', '~> 7.0.8' # The original asset pipeline for Rails [https://github.com/rails/sprockets-rails] -gem "sprockets-rails" +gem 'sprockets-rails' # Use postgresql as the database for Active Record -gem "pg", "~> 1.1" +gem 'pg', '~> 1.1' # Use the Puma web server [https://github.com/puma/puma] -gem "puma", "~> 5.0" +gem 'puma', '~> 5.0' # Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails] -gem "importmap-rails" +gem 'importmap-rails' # Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev] -gem "turbo-rails" +gem 'turbo-rails' # Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev] -gem "stimulus-rails" +gem 'stimulus-rails' # Build JSON APIs with ease [https://github.com/rails/jbuilder] -gem "jbuilder" +gem 'jbuilder' # Use Redis adapter to run Action Cable in production # gem "redis", "~> 4.0" @@ -37,10 +37,10 @@ gem "jbuilder" # gem "bcrypt", "~> 3.1.7" # Windows does not include zoneinfo files, so bundle the tzinfo-data gem -gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ] +gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby] # Reduces boot times through caching; required in config/boot.rb -gem "bootsnap", require: false +gem 'bootsnap', require: false # Use Sass to process CSS # gem "sassc-rails" @@ -50,12 +50,12 @@ gem "bootsnap", require: false group :development, :test do # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem - gem "debug", platforms: %i[ mri mingw x64_mingw ] + gem 'debug', platforms: %i[mri mingw x64_mingw] end group :development do # Use console on exceptions pages [https://github.com/rails/web-console] - gem "web-console" + gem 'web-console' # Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler] # gem "rack-mini-profiler" @@ -66,9 +66,10 @@ end group :test do # Use system testing [https://guides.rubyonrails.org/testing.html#system-testing] - gem "capybara" - gem "selenium-webdriver" - + gem 'capybara' + gem 'selenium-webdriver' end -gem "devise", "~> 4.9" +gem 'devise', '~> 4.9' + +gem 'rubocop', '>= 1.0', '< 2.0' diff --git a/Gemfile.lock b/Gemfile.lock index 56531a6..87c665d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -68,6 +68,8 @@ GEM tzinfo (~> 2.0) addressable (2.8.5) public_suffix (>= 2.0.2, < 6.0) + ast (2.4.2) + base64 (0.1.1) bcrypt (3.1.19) bindex (0.8.1) bootsnap (1.16.0) @@ -109,6 +111,8 @@ GEM jbuilder (2.11.5) actionview (>= 5.0.0) activesupport (>= 5.0.0) + json (2.6.3) + language_server-protocol (3.17.0.3) loofah (2.21.4) crass (~> 1.0.2) nokogiri (>= 1.12.0) @@ -136,6 +140,10 @@ GEM nokogiri (1.15.4-x64-mingw-ucrt) racc (~> 1.4) orm_adapter (0.5.0) + parallel (1.23.0) + parser (3.2.2.4) + ast (~> 2.4.1) + racc pg (1.5.4-x64-mingw-ucrt) psych (5.1.1.1) stringio @@ -174,6 +182,7 @@ GEM rake (>= 12.2) thor (~> 1.0) zeitwerk (~> 2.5) + rainbow (3.1.1) rake (13.0.6) rdoc (6.5.0) psych (>= 4.0.0) @@ -184,6 +193,21 @@ GEM actionpack (>= 5.2) railties (>= 5.2) rexml (3.2.6) + rubocop (1.56.4) + base64 (~> 0.1.1) + json (~> 2.3) + language_server-protocol (>= 3.17.0) + parallel (~> 1.10) + parser (>= 3.2.2.3) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.28.1, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.29.0) + parser (>= 3.2.1.0) + ruby-progressbar (1.13.0) rubyzip (2.3.2) selenium-webdriver (4.14.0) rexml (~> 3.2, >= 3.2.5) @@ -209,6 +233,7 @@ GEM concurrent-ruby (~> 1.0) tzinfo-data (1.2023.3) tzinfo (>= 1.0.0) + unicode-display_width (2.5.0) warden (1.2.9) rack (>= 2.0.9) web-console (4.2.1) @@ -237,6 +262,7 @@ DEPENDENCIES pg (~> 1.1) puma (~> 5.0) rails (~> 7.0.8) + rubocop (>= 1.0, < 2.0) selenium-webdriver sprockets-rails stimulus-rails diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index a4c8998..716e300 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -70,6 +70,7 @@ main { gap: 1rem; padding: 10%; } + /* #5fb523 #434b54 */ #logout { background-color: #3778c2; @@ -80,6 +81,16 @@ main { border-bottom: 1px solid #fff; } +.btn { + display: grid; + place-items: center; + border-radius: 3px; + padding: 0.8rem; + width: 94%; + margin: 0 auto; + margin-top: 10px; +} + #logout .btn { background-color: red; color: #fff; @@ -93,16 +104,6 @@ main { cursor: pointer; } -.btn { - display: grid; - place-items: center; - border-radius: 3px; - padding: 0.8rem; - width: 94%; - margin: 0 auto; - margin-top: 10px; -} - .btn.pri, input[type='submit'] { background-color: #3778c2; @@ -172,7 +173,7 @@ input, button { outline: none; border: none; - padding: 0.8rem 0rem; + padding: 0.8rem 0; } .field input { @@ -242,6 +243,6 @@ button { } #date { - opacity: .5; + opacity: 0.5; font-size: 12px; -} \ No newline at end of file +} diff --git a/app/controllers/entities_controller.rb b/app/controllers/entities_controller.rb index 1c360ba..b97c050 100644 --- a/app/controllers/entities_controller.rb +++ b/app/controllers/entities_controller.rb @@ -1,7 +1,7 @@ class EntitiesController < ApplicationController before_action :authenticate_user! - before_action :set_group, only: %i[ new create ] - before_action :set_entity, only: %i[ show edit update destroy ] + before_action :set_group, only: %i[new create] + before_action :set_entity, only: %i[show edit update destroy] # GET /entities/new def new @@ -17,7 +17,7 @@ def create respond_to do |format| if @entity.save - format.html { redirect_to group_path(@group), notice: "Entity was successfully created." } + format.html { redirect_to group_path(@group), notice: 'Entity was successfully created.' } else format.html { render :new, status: :unprocessable_entity } end @@ -25,17 +25,18 @@ def create end private - # Use callbacks to share common setup or constraints between actions. - def set_entity - @entity = Entity.find(params[:id]) - end - def set_group - @group = Group.find(params[:group_id]) - end + # Use callbacks to share common setup or constraints between actions. + def set_entity + @entity = Entity.find(params[:id]) + end - # Only allow a list of trusted parameters through. - def entity_params - params.require(:entity).permit(:name, :amount) - end + def set_group + @group = Group.find(params[:group_id]) + end + + # Only allow a list of trusted parameters through. + def entity_params + params.require(:entity).permit(:name, :amount) + end end diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index 30798a1..4049db9 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -1,6 +1,6 @@ class GroupsController < ApplicationController before_action :authenticate_user! - before_action :set_group, only: %i[ show edit update destroy ] + before_action :set_group, only: %i[show edit update destroy] # GET /groups or /groups.json def index @@ -18,8 +18,7 @@ def new end # GET /groups/1/edit - def edit - end + def edit; end # POST /groups or /groups.json def create @@ -28,7 +27,7 @@ def create respond_to do |format| if @group.save - format.html { redirect_to group_url(@group), notice: "Group was successfully created." } + format.html { redirect_to group_url(@group), notice: 'Group was successfully created.' } else format.html { render :new, status: :unprocessable_entity } end @@ -39,7 +38,7 @@ def create def update respond_to do |format| if @group.update(group_params) - format.html { redirect_to group_url(@group), notice: "Group was successfully updated." } + format.html { redirect_to group_url(@group), notice: 'Group was successfully updated.' } format.json { render :show, status: :ok, location: @group } else format.html { render :edit, status: :unprocessable_entity } @@ -53,19 +52,20 @@ def destroy @group.destroy respond_to do |format| - format.html { redirect_to groups_url, notice: "Group was successfully destroyed." } + format.html { redirect_to groups_url, notice: 'Group was successfully destroyed.' } format.json { head :no_content } end end private - # Use callbacks to share common setup or constraints between actions. - def set_group - @group = Group.find(params[:id]) - end - # Only allow a list of trusted parameters through. - def group_params - params.require(:group).permit(:name, :icon) - end + # Use callbacks to share common setup or constraints between actions. + def set_group + @group = Group.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def group_params + params.require(:group).permit(:name, :icon) + end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index c7a40ee..7b17fd4 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,5 +1,5 @@ class UsersController < ApplicationController - before_action :set_user, only: %i[ show edit update destroy ] + before_action :set_user, only: %i[show edit update destroy] # GET /users/new def new @@ -12,7 +12,7 @@ def create respond_to do |format| if @user.save - format.html { redirect_to user_url(@user), notice: "User was successfully created." } + format.html { redirect_to user_url(@user), notice: 'User was successfully created.' } format.json { render :show, status: :created, location: @user } else format.html { render :new, status: :unprocessable_entity } @@ -25,7 +25,7 @@ def create def update respond_to do |format| if @user.update(user_params) - format.html { redirect_to user_url(@user), notice: "User was successfully updated." } + format.html { redirect_to user_url(@user), notice: 'User was successfully updated.' } format.json { render :show, status: :ok, location: @user } else format.html { render :edit, status: :unprocessable_entity } @@ -39,19 +39,20 @@ def destroy @user.destroy respond_to do |format| - format.html { redirect_to users_url, notice: "User was successfully destroyed." } + format.html { redirect_to users_url, notice: 'User was successfully destroyed.' } format.json { head :no_content } end end private - # Use callbacks to share common setup or constraints between actions. - def set_user - @user = User.find(params[:id]) - end - # Only allow a list of trusted parameters through. - def user_params - params.require(:user).permit(:name) - end + # Use callbacks to share common setup or constraints between actions. + def set_user + @user = User.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def user_params + params.require(:user).permit(:name) + end end diff --git a/app/jobs/application_job.rb b/app/jobs/application_job.rb deleted file mode 100644 index d394c3d..0000000 --- a/app/jobs/application_job.rb +++ /dev/null @@ -1,7 +0,0 @@ -class ApplicationJob < ActiveJob::Base - # Automatically retry jobs that encountered a deadlock - # retry_on ActiveRecord::Deadlocked - - # Most jobs are safe to ignore if the underlying records are no longer available - # discard_on ActiveJob::DeserializationError -end diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 3c34c81..286b223 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -1,4 +1,4 @@ class ApplicationMailer < ActionMailer::Base - default from: "from@example.com" - layout "mailer" + default from: 'from@example.com' + layout 'mailer' end diff --git a/app/models/entity.rb b/app/models/entity.rb index bd5749c..e87bb86 100644 --- a/app/models/entity.rb +++ b/app/models/entity.rb @@ -1,4 +1,4 @@ class Entity < ApplicationRecord - validates :name, presence: true - has_and_belongs_to_many :groups + validates :name, presence: true + has_and_belongs_to_many :groups end diff --git a/app/models/group.rb b/app/models/group.rb index 7e234fe..486a5aa 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -1,6 +1,6 @@ class Group < ApplicationRecord - validates :name, presence: true - validates :icon, presence: true - belongs_to :user - has_and_belongs_to_many :entities + validates :name, presence: true + validates :icon, presence: true + belongs_to :user + has_and_belongs_to_many :entities end diff --git a/app/views/entities/_entity.json.jbuilder b/app/views/entities/_entity.json.jbuilder deleted file mode 100644 index f822f3c..0000000 --- a/app/views/entities/_entity.json.jbuilder +++ /dev/null @@ -1,2 +0,0 @@ -json.extract! entity, :id, :name, :amount, :created_at, :updated_at -json.url entity_url(entity, format: :json) diff --git a/app/views/entities/edit.html.erb b/app/views/entities/edit.html.erb deleted file mode 100644 index 6d838ce..0000000 --- a/app/views/entities/edit.html.erb +++ /dev/null @@ -1,10 +0,0 @@ -

Editing entity

- -<%= render "form", entity: @entity %> - -
- -
- <%= link_to "Show this entity", @entity %> | - <%= link_to "Back to entities", entities_path %> -
diff --git a/app/views/entities/index.html.erb b/app/views/entities/index.html.erb deleted file mode 100644 index a8a3051..0000000 --- a/app/views/entities/index.html.erb +++ /dev/null @@ -1,14 +0,0 @@ -

<%= notice %>

- -

Entities

- -
- <% @entities.each do |entity| %> - <%= render entity %> -

- <%= link_to "Show this entity", entity %> -

- <% end %> -
- -<%= link_to "New entity", new_entity_path %> diff --git a/app/views/entities/index.json.jbuilder b/app/views/entities/index.json.jbuilder deleted file mode 100644 index 955ceb3..0000000 --- a/app/views/entities/index.json.jbuilder +++ /dev/null @@ -1 +0,0 @@ -json.array! @entities, partial: "entities/entity", as: :entity diff --git a/app/views/entities/show.html.erb b/app/views/entities/show.html.erb deleted file mode 100644 index e1405a5..0000000 --- a/app/views/entities/show.html.erb +++ /dev/null @@ -1,10 +0,0 @@ -

<%= notice %>

- -<%= render @entity %> - -
- <%= link_to "Edit this entity", edit_entity_path(@entity) %> | - <%= link_to "Back to entities", entities_path %> - - <%= button_to "Destroy this entity", @entity, method: :delete %> -
diff --git a/app/views/entities/show.json.jbuilder b/app/views/entities/show.json.jbuilder index 0bb9794..0337c8d 100644 --- a/app/views/entities/show.json.jbuilder +++ b/app/views/entities/show.json.jbuilder @@ -1 +1 @@ -json.partial! "entities/entity", entity: @entity +json.partial! 'entities/entity', entity: @entity diff --git a/app/views/groups/_group.json.jbuilder b/app/views/groups/_group.json.jbuilder deleted file mode 100644 index 2abac69..0000000 --- a/app/views/groups/_group.json.jbuilder +++ /dev/null @@ -1,2 +0,0 @@ -json.extract! group, :id, :name, :icon, :created_at, :updated_at -json.url group_url(group, format: :json) diff --git a/app/views/groups/edit.html.erb b/app/views/groups/edit.html.erb deleted file mode 100644 index 23fe74b..0000000 --- a/app/views/groups/edit.html.erb +++ /dev/null @@ -1,10 +0,0 @@ -

Editing group

- -<%= render "form", group: @group %> - -
- -
- <%= link_to "Show this group", @group %> | - <%= link_to "Back to groups", groups_path %> -
diff --git a/app/views/groups/index.json.jbuilder b/app/views/groups/index.json.jbuilder index 37cc188..e4c6577 100644 --- a/app/views/groups/index.json.jbuilder +++ b/app/views/groups/index.json.jbuilder @@ -1 +1 @@ -json.array! @groups, partial: "groups/group", as: :group +json.array! @groups, partial: 'groups/group', as: :group diff --git a/app/views/groups/show.json.jbuilder b/app/views/groups/show.json.jbuilder index 76d7638..476ed6e 100644 --- a/app/views/groups/show.json.jbuilder +++ b/app/views/groups/show.json.jbuilder @@ -1 +1 @@ -json.partial! "groups/group", group: @group +json.partial! 'groups/group', group: @group diff --git a/app/views/users/_form.html.erb b/app/views/users/_form.html.erb deleted file mode 100644 index e35c1f2..0000000 --- a/app/views/users/_form.html.erb +++ /dev/null @@ -1,22 +0,0 @@ -<%= form_with(model: user) do |form| %> - <% if user.errors.any? %> -
-

<%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:

- -
    - <% user.errors.each do |error| %> -
  • <%= error.full_message %>
  • - <% end %> -
-
- <% end %> - -
- <%= form.label :name, style: "display: block" %> - <%= form.text_field :name %> -
- -
- <%= form.submit %> -
-<% end %> diff --git a/app/views/users/_user.html.erb b/app/views/users/_user.html.erb deleted file mode 100644 index 5d2c950..0000000 --- a/app/views/users/_user.html.erb +++ /dev/null @@ -1,7 +0,0 @@ -
-

- Name: - <%= user.name %> -

- -
diff --git a/app/views/users/_user.json.jbuilder b/app/views/users/_user.json.jbuilder deleted file mode 100644 index df793f1..0000000 --- a/app/views/users/_user.json.jbuilder +++ /dev/null @@ -1,2 +0,0 @@ -json.extract! user, :id, :name, :created_at, :updated_at -json.url user_url(user, format: :json) diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb deleted file mode 100644 index 9dda632..0000000 --- a/app/views/users/edit.html.erb +++ /dev/null @@ -1,10 +0,0 @@ -

Editing user

- -<%= render "form", user: @user %> - -
- -
- <%= link_to "Show this user", @user %> | - <%= link_to "Back to users", users_path %> -
diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb deleted file mode 100644 index fe4dd5c..0000000 --- a/app/views/users/index.html.erb +++ /dev/null @@ -1,14 +0,0 @@ -

<%= notice %>

- -

Users

- -
- <% @users.each do |user| %> - <%= render user %> -

- <%= link_to "Show this user", user %> -

- <% end %> -
- -<%= link_to "New user", new_user_path %> diff --git a/app/views/users/index.json.jbuilder b/app/views/users/index.json.jbuilder index 98788da..2faf5af 100644 --- a/app/views/users/index.json.jbuilder +++ b/app/views/users/index.json.jbuilder @@ -1 +1 @@ -json.array! @users, partial: "users/user", as: :user +json.array! @users, partial: 'users/user', as: :user diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb deleted file mode 100644 index eedbd83..0000000 --- a/app/views/users/new.html.erb +++ /dev/null @@ -1,9 +0,0 @@ -

New user

- -<%= render "form", user: @user %> - -
- -
- <%= link_to "Back to users", users_path %> -
diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb deleted file mode 100644 index 673fae2..0000000 --- a/app/views/users/show.html.erb +++ /dev/null @@ -1,10 +0,0 @@ -

<%= notice %>

- -<%= render @user %> - -
- <%= link_to "Edit this user", edit_user_path(@user) %> | - <%= link_to "Back to users", users_path %> - - <%= button_to "Destroy this user", @user, method: :delete %> -
diff --git a/app/views/users/show.json.jbuilder b/app/views/users/show.json.jbuilder index ff40bb9..2a33f71 100644 --- a/app/views/users/show.json.jbuilder +++ b/app/views/users/show.json.jbuilder @@ -1 +1 @@ -json.partial! "users/user", user: @user +json.partial! 'users/user', user: @user diff --git a/config.ru b/config.ru index 4a3c09a..ad1fbf2 100644 --- a/config.ru +++ b/config.ru @@ -1,6 +1,6 @@ # This file is used by Rack-based servers to start the application. -require_relative "config/environment" +require_relative 'config/environment' run Rails.application Rails.application.load_server diff --git a/test/application_system_test_case.rb b/test/application_system_test_case.rb index d19212a..23701b4 100644 --- a/test/application_system_test_case.rb +++ b/test/application_system_test_case.rb @@ -1,4 +1,4 @@ -require "test_helper" +require 'test_helper' class ApplicationSystemTestCase < ActionDispatch::SystemTestCase driven_by :selenium, using: :chrome, screen_size: [1400, 1400] diff --git a/test/channels/application_cable/connection_test.rb b/test/channels/application_cable/connection_test.rb index 800405f..d05dbd2 100644 --- a/test/channels/application_cable/connection_test.rb +++ b/test/channels/application_cable/connection_test.rb @@ -1,4 +1,4 @@ -require "test_helper" +require 'test_helper' class ApplicationCable::ConnectionTest < ActionCable::Connection::TestCase # test "connects with cookies" do diff --git a/test/controllers/entities_controller_test.rb b/test/controllers/entities_controller_test.rb index d1f0592..fd252da 100644 --- a/test/controllers/entities_controller_test.rb +++ b/test/controllers/entities_controller_test.rb @@ -1,45 +1,45 @@ -require "test_helper" +require 'test_helper' class EntitiesControllerTest < ActionDispatch::IntegrationTest setup do @entity = entities(:one) end - test "should get index" do + test 'should get index' do get entities_url assert_response :success end - test "should get new" do + test 'should get new' do get new_entity_url assert_response :success end - test "should create entity" do - assert_difference("Entity.count") do + test 'should create entity' do + assert_difference('Entity.count') do post entities_url, params: { entity: { amount: @entity.amount, name: @entity.name } } end assert_redirected_to entity_url(Entity.last) end - test "should show entity" do + test 'should show entity' do get entity_url(@entity) assert_response :success end - test "should get edit" do + test 'should get edit' do get edit_entity_url(@entity) assert_response :success end - test "should update entity" do + test 'should update entity' do patch entity_url(@entity), params: { entity: { amount: @entity.amount, name: @entity.name } } assert_redirected_to entity_url(@entity) end - test "should destroy entity" do - assert_difference("Entity.count", -1) do + test 'should destroy entity' do + assert_difference('Entity.count', -1) do delete entity_url(@entity) end diff --git a/test/controllers/groups_controller_test.rb b/test/controllers/groups_controller_test.rb index 823add0..17d312d 100644 --- a/test/controllers/groups_controller_test.rb +++ b/test/controllers/groups_controller_test.rb @@ -1,45 +1,45 @@ -require "test_helper" +require 'test_helper' class GroupsControllerTest < ActionDispatch::IntegrationTest setup do @group = groups(:one) end - test "should get index" do + test 'should get index' do get groups_url assert_response :success end - test "should get new" do + test 'should get new' do get new_group_url assert_response :success end - test "should create group" do - assert_difference("Group.count") do + test 'should create group' do + assert_difference('Group.count') do post groups_url, params: { group: { icon: @group.icon, name: @group.name } } end assert_redirected_to group_url(Group.last) end - test "should show group" do + test 'should show group' do get group_url(@group) assert_response :success end - test "should get edit" do + test 'should get edit' do get edit_group_url(@group) assert_response :success end - test "should update group" do + test 'should update group' do patch group_url(@group), params: { group: { icon: @group.icon, name: @group.name } } assert_redirected_to group_url(@group) end - test "should destroy group" do - assert_difference("Group.count", -1) do + test 'should destroy group' do + assert_difference('Group.count', -1) do delete group_url(@group) end diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index 7ebfb8f..6d44c48 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -1,45 +1,45 @@ -require "test_helper" +require 'test_helper' class UsersControllerTest < ActionDispatch::IntegrationTest setup do @user = users(:one) end - test "should get index" do + test 'should get index' do get users_url assert_response :success end - test "should get new" do + test 'should get new' do get new_user_url assert_response :success end - test "should create user" do - assert_difference("User.count") do + test 'should create user' do + assert_difference('User.count') do post users_url, params: { user: { name: @user.name } } end assert_redirected_to user_url(User.last) end - test "should show user" do + test 'should show user' do get user_url(@user) assert_response :success end - test "should get edit" do + test 'should get edit' do get edit_user_url(@user) assert_response :success end - test "should update user" do + test 'should update user' do patch user_url(@user), params: { user: { name: @user.name } } assert_redirected_to user_url(@user) end - test "should destroy user" do - assert_difference("User.count", -1) do + test 'should destroy user' do + assert_difference('User.count', -1) do delete user_url(@user) end diff --git a/test/models/entity_test.rb b/test/models/entity_test.rb index 4c61596..cf5519f 100644 --- a/test/models/entity_test.rb +++ b/test/models/entity_test.rb @@ -1,4 +1,4 @@ -require "test_helper" +require 'test_helper' class EntityTest < ActiveSupport::TestCase # test "the truth" do diff --git a/test/models/group_test.rb b/test/models/group_test.rb index eddbcc8..778eb0c 100644 --- a/test/models/group_test.rb +++ b/test/models/group_test.rb @@ -1,4 +1,4 @@ -require "test_helper" +require 'test_helper' class GroupTest < ActiveSupport::TestCase # test "the truth" do diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 5c07f49..82f61e0 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -1,4 +1,4 @@ -require "test_helper" +require 'test_helper' class UserTest < ActiveSupport::TestCase # test "the truth" do diff --git a/test/system/entities_test.rb b/test/system/entities_test.rb index 3d1df50..7781c07 100644 --- a/test/system/entities_test.rb +++ b/test/system/entities_test.rb @@ -1,43 +1,43 @@ -require "application_system_test_case" +require 'application_system_test_case' class EntitiesTest < ApplicationSystemTestCase setup do @entity = entities(:one) end - test "visiting the index" do + test 'visiting the index' do visit entities_url - assert_selector "h1", text: "Entities" + assert_selector 'h1', text: 'Entities' end - test "should create entity" do + test 'should create entity' do visit entities_url - click_on "New entity" + click_on 'New entity' - fill_in "Amount", with: @entity.amount - fill_in "Name", with: @entity.name - click_on "Create Entity" + fill_in 'Amount', with: @entity.amount + fill_in 'Name', with: @entity.name + click_on 'Create Entity' - assert_text "Entity was successfully created" - click_on "Back" + assert_text 'Entity was successfully created' + click_on 'Back' end - test "should update Entity" do + test 'should update Entity' do visit entity_url(@entity) - click_on "Edit this entity", match: :first + click_on 'Edit this entity', match: :first - fill_in "Amount", with: @entity.amount - fill_in "Name", with: @entity.name - click_on "Update Entity" + fill_in 'Amount', with: @entity.amount + fill_in 'Name', with: @entity.name + click_on 'Update Entity' - assert_text "Entity was successfully updated" - click_on "Back" + assert_text 'Entity was successfully updated' + click_on 'Back' end - test "should destroy Entity" do + test 'should destroy Entity' do visit entity_url(@entity) - click_on "Destroy this entity", match: :first + click_on 'Destroy this entity', match: :first - assert_text "Entity was successfully destroyed" + assert_text 'Entity was successfully destroyed' end end diff --git a/test/system/groups_test.rb b/test/system/groups_test.rb index db8864d..67e53ef 100644 --- a/test/system/groups_test.rb +++ b/test/system/groups_test.rb @@ -1,43 +1,43 @@ -require "application_system_test_case" +require 'application_system_test_case' class GroupsTest < ApplicationSystemTestCase setup do @group = groups(:one) end - test "visiting the index" do + test 'visiting the index' do visit groups_url - assert_selector "h1", text: "Groups" + assert_selector 'h1', text: 'Groups' end - test "should create group" do + test 'should create group' do visit groups_url - click_on "New group" + click_on 'New group' - fill_in "Icon", with: @group.icon - fill_in "Name", with: @group.name - click_on "Create Group" + fill_in 'Icon', with: @group.icon + fill_in 'Name', with: @group.name + click_on 'Create Group' - assert_text "Group was successfully created" - click_on "Back" + assert_text 'Group was successfully created' + click_on 'Back' end - test "should update Group" do + test 'should update Group' do visit group_url(@group) - click_on "Edit this group", match: :first + click_on 'Edit this group', match: :first - fill_in "Icon", with: @group.icon - fill_in "Name", with: @group.name - click_on "Update Group" + fill_in 'Icon', with: @group.icon + fill_in 'Name', with: @group.name + click_on 'Update Group' - assert_text "Group was successfully updated" - click_on "Back" + assert_text 'Group was successfully updated' + click_on 'Back' end - test "should destroy Group" do + test 'should destroy Group' do visit group_url(@group) - click_on "Destroy this group", match: :first + click_on 'Destroy this group', match: :first - assert_text "Group was successfully destroyed" + assert_text 'Group was successfully destroyed' end end diff --git a/test/system/users_test.rb b/test/system/users_test.rb index 3553c0d..00d405b 100644 --- a/test/system/users_test.rb +++ b/test/system/users_test.rb @@ -1,41 +1,41 @@ -require "application_system_test_case" +require 'application_system_test_case' class UsersTest < ApplicationSystemTestCase setup do @user = users(:one) end - test "visiting the index" do + test 'visiting the index' do visit users_url - assert_selector "h1", text: "Users" + assert_selector 'h1', text: 'Users' end - test "should create user" do + test 'should create user' do visit users_url - click_on "New user" + click_on 'New user' - fill_in "Name", with: @user.name - click_on "Create User" + fill_in 'Name', with: @user.name + click_on 'Create User' - assert_text "User was successfully created" - click_on "Back" + assert_text 'User was successfully created' + click_on 'Back' end - test "should update User" do + test 'should update User' do visit user_url(@user) - click_on "Edit this user", match: :first + click_on 'Edit this user', match: :first - fill_in "Name", with: @user.name - click_on "Update User" + fill_in 'Name', with: @user.name + click_on 'Update User' - assert_text "User was successfully updated" - click_on "Back" + assert_text 'User was successfully updated' + click_on 'Back' end - test "should destroy User" do + test 'should destroy User' do visit user_url(@user) - click_on "Destroy this user", match: :first + click_on 'Destroy this user', match: :first - assert_text "User was successfully destroyed" + assert_text 'User was successfully destroyed' end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 973526e..91c1291 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,6 +1,6 @@ -ENV["RAILS_ENV"] ||= "test" -require_relative "../config/environment" -require "rails/test_help" +ENV['RAILS_ENV'] ||= 'test' +require_relative '../config/environment' +require 'rails/test_help' class ActiveSupport::TestCase # Run tests in parallel with specified workers From 7c1d1c0815547c1f5dd8066e3b3cafbadaa1972d Mon Sep 17 00:00:00 2001 From: hassanShakur Date: Thu, 19 Oct 2023 17:10:29 +0300 Subject: [PATCH 14/39] add group multiselect for new entity --- app/controllers/entities_controller.rb | 11 ++--------- app/models/entity.rb | 2 ++ app/views/entities/_form.html.erb | 6 +++++- app/views/entities/new.html.erb | 2 +- app/views/groups/show.html.erb | 2 +- config/routes.rb | 8 ++------ 6 files changed, 13 insertions(+), 18 deletions(-) diff --git a/app/controllers/entities_controller.rb b/app/controllers/entities_controller.rb index b97c050..b4474cf 100644 --- a/app/controllers/entities_controller.rb +++ b/app/controllers/entities_controller.rb @@ -1,23 +1,20 @@ class EntitiesController < ApplicationController before_action :authenticate_user! - before_action :set_group, only: %i[new create] before_action :set_entity, only: %i[show edit update destroy] # GET /entities/new def new @entity = Entity.new - @group = Group.find(params[:group_id]) end # POST /entities or /entities.json def create @entity = Entity.new(entity_params) - @entity.groups << @group @entity.author_id = current_user.id respond_to do |format| if @entity.save - format.html { redirect_to group_path(@group), notice: 'Entity was successfully created.' } + format.html { redirect_to groups_path, notice: 'Entity was successfully created.' } else format.html { render :new, status: :unprocessable_entity } end @@ -31,12 +28,8 @@ def set_entity @entity = Entity.find(params[:id]) end - def set_group - @group = Group.find(params[:group_id]) - end - # Only allow a list of trusted parameters through. def entity_params - params.require(:entity).permit(:name, :amount) + params.require(:entity).permit(:name, :amount, group_ids: []) end end diff --git a/app/models/entity.rb b/app/models/entity.rb index e87bb86..1dad8f5 100644 --- a/app/models/entity.rb +++ b/app/models/entity.rb @@ -1,4 +1,6 @@ class Entity < ApplicationRecord validates :name, presence: true + validates :groups, presence: true + validates :amount, presence: true, numericality: { greater_than: 0 } has_and_belongs_to_many :groups end diff --git a/app/views/entities/_form.html.erb b/app/views/entities/_form.html.erb index 824048c..20d1999 100644 --- a/app/views/entities/_form.html.erb +++ b/app/views/entities/_form.html.erb @@ -1,4 +1,4 @@ -<%= form_with model: entity, url: group_entities_path(@group), local: true do |form| %> +<%= form_with model: entity, local: true do |form| %> <% if entity.errors.any? %>

<%= pluralize(entity.errors.count, "error") %> prohibited this entity from being saved:

@@ -19,6 +19,10 @@ <%= form.text_field :amount, placeholder: 'Amount' %>
+
+ <%= form.collection_select :group_ids, Group.all, :id, :name, {prompt: 'Select Group'}, {multiple: true} %> +
+
<%= form.submit %>
diff --git a/app/views/entities/new.html.erb b/app/views/entities/new.html.erb index 6ceaf44..04db643 100644 --- a/app/views/entities/new.html.erb +++ b/app/views/entities/new.html.erb @@ -6,6 +6,6 @@
- <%= link_to "Back to transactions", group_path(@group), id: 'back' %> + <%= link_to "Back to transactions", groups_path, id: 'back' %>
diff --git a/app/views/groups/show.html.erb b/app/views/groups/show.html.erb index 53475e1..b882874 100644 --- a/app/views/groups/show.html.erb +++ b/app/views/groups/show.html.erb @@ -18,7 +18,7 @@
<%= render partial: 'entities/entity', collection: @group.entities %> - <%= link_to "Add a new transaction", new_group_entity_path(@group), class: 'action btn' %> + <%= link_to "Add a new transaction", new_entity_path, class: 'action btn' %>
diff --git a/config/routes.rb b/config/routes.rb index 6564631..9484a59 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,13 +1,9 @@ Rails.application.routes.draw do devise_for :users - resources :groups, only: %i[index show new create] do - resources :entities, only: %i[create new] - end - - get '/groups/:group_id/entities/new_form', to: 'entities#new', as: 'new_group_entity_form' + resources :groups, only: %i[index show new create] + resources :entities, only: %i[create new] - # make users#splash the root path only if the user is not signed in, else make groups#index the root path authenticated :user do root 'groups#index', as: :authenticated_root end From 3df0e0041eb82e9b45f6a4f45cd23355b4562908 Mon Sep 17 00:00:00 2001 From: hassanShakur Date: Thu, 19 Oct 2023 17:19:07 +0300 Subject: [PATCH 15/39] fix UI disalignment --- app/assets/stylesheets/application.css | 4 ++-- app/controllers/groups_controller.rb | 2 +- app/views/entities/_form.html.erb | 1 + 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 716e300..caffc6b 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -86,7 +86,7 @@ main { place-items: center; border-radius: 3px; padding: 0.8rem; - width: 94%; + width: 90%; margin: 0 auto; margin-top: 10px; } @@ -109,7 +109,7 @@ input[type='submit'] { background-color: #3778c2; color: #fff; padding: 0.8rem; - margin: 10px 0; + margin: 10px auto; } .btn.pri:hover { diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index 4049db9..459fe8c 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -27,7 +27,7 @@ def create respond_to do |format| if @group.save - format.html { redirect_to group_url(@group), notice: 'Group was successfully created.' } + format.html { redirect_to groups_url, notice: 'Group was successfully created.' } else format.html { render :new, status: :unprocessable_entity } end diff --git a/app/views/entities/_form.html.erb b/app/views/entities/_form.html.erb index 20d1999..5edcd23 100644 --- a/app/views/entities/_form.html.erb +++ b/app/views/entities/_form.html.erb @@ -20,6 +20,7 @@
+ <%= form.label :group_ids, 'Groups (Hold Ctrl & select)' %> <%= form.collection_select :group_ids, Group.all, :id, :name, {prompt: 'Select Group'}, {multiple: true} %>
From 3900af945ea791d03309646623cf79b5f11ea298 Mon Sep 17 00:00:00 2001 From: hassanShakur Date: Thu, 19 Oct 2023 17:49:11 +0300 Subject: [PATCH 16/39] final tweaks and design --- app/assets/stylesheets/application.css | 10 +++++++++- app/controllers/entities_controller.rb | 7 ++++++- app/views/entities/_form.html.erb | 2 +- app/views/entities/new.html.erb | 2 +- app/views/groups/show.html.erb | 6 +++--- app/views/layouts/application.html.erb | 5 +++-- app/views/users/splash.html.erb | 2 +- config/routes.rb | 8 ++++++-- 8 files changed, 30 insertions(+), 12 deletions(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index caffc6b..efc37e5 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -33,7 +33,7 @@ body { main { max-width: 500px; margin: 0 auto; - padding: 0; + padding-bottom: 2rem; font-family: 'Roboto', sans-serif; font-size: 16px; color: #434b54; @@ -43,6 +43,14 @@ main { background-color: #fff; } +#notice, +#alert { + text-align: center; + padding: 0.5rem; + background-color: #3778c2; + color: #fff; +} + .header { background-color: #3778c2; color: #fff; diff --git a/app/controllers/entities_controller.rb b/app/controllers/entities_controller.rb index b4474cf..bca7c20 100644 --- a/app/controllers/entities_controller.rb +++ b/app/controllers/entities_controller.rb @@ -1,5 +1,6 @@ class EntitiesController < ApplicationController before_action :authenticate_user! + before_action :set_group, only: %i[new create] before_action :set_entity, only: %i[show edit update destroy] # GET /entities/new @@ -14,7 +15,7 @@ def create respond_to do |format| if @entity.save - format.html { redirect_to groups_path, notice: 'Entity was successfully created.' } + format.html { redirect_to group_path(@group), notice: 'Entity was successfully created.' } else format.html { render :new, status: :unprocessable_entity } end @@ -28,6 +29,10 @@ def set_entity @entity = Entity.find(params[:id]) end + def set_group + @group = Group.find(params[:group_id]) + end + # Only allow a list of trusted parameters through. def entity_params params.require(:entity).permit(:name, :amount, group_ids: []) diff --git a/app/views/entities/_form.html.erb b/app/views/entities/_form.html.erb index 5edcd23..9c31e09 100644 --- a/app/views/entities/_form.html.erb +++ b/app/views/entities/_form.html.erb @@ -1,4 +1,4 @@ -<%= form_with model: entity, local: true do |form| %> +<%= form_with model: entity, url: group_entities_path(@group), local: true do |form| %> <% if entity.errors.any? %>

<%= pluralize(entity.errors.count, "error") %> prohibited this entity from being saved:

diff --git a/app/views/entities/new.html.erb b/app/views/entities/new.html.erb index 04db643..65c9e4f 100644 --- a/app/views/entities/new.html.erb +++ b/app/views/entities/new.html.erb @@ -6,6 +6,6 @@
- <%= link_to "Back to transactions", groups_path, id: 'back' %> + <%= link_to "Back to transactions", group_path(@group) %>
diff --git a/app/views/groups/show.html.erb b/app/views/groups/show.html.erb index b882874..712e1c6 100644 --- a/app/views/groups/show.html.erb +++ b/app/views/groups/show.html.erb @@ -1,5 +1,5 @@
- <%= link_to "Back", groups_path %> + <%= link_to "Back", authenticated_root_path %>
@@ -16,9 +16,9 @@
- <%= render partial: 'entities/entity', collection: @group.entities %> + <%= render partial: 'entities/entity', collection: @group.entities.order(created_at: :desc) %> - <%= link_to "Add a new transaction", new_entity_path, class: 'action btn' %> + <%= link_to "Add a new transaction", new_group_entity_path(@group), class: 'action btn' %>
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 500a263..e91000b 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,7 +1,7 @@ - Budgeter + Budgetize <%= csrf_meta_tags %> <%= csp_meta_tag %> @@ -12,7 +12,8 @@
-

<%= notice %>

+

<%= notice %>

+

<%= alert %>

<% if user_signed_in? then %>
<%= button_to "Logout" , destroy_user_session_path, method: :delete, class: 'btn' %> diff --git a/app/views/users/splash.html.erb b/app/views/users/splash.html.erb index 793f1c9..56da92b 100644 --- a/app/views/users/splash.html.erb +++ b/app/views/users/splash.html.erb @@ -1,5 +1,5 @@
-

Budgeter

+

Budgetize