From 49c74aeea3aeb1ce7f12e37ecb22044cb4bf765b Mon Sep 17 00:00:00 2001 From: Adrian Hinz Date: Fri, 11 Apr 2025 20:47:11 +0200 Subject: [PATCH] Added Clients and Client Notes --- app/controllers/clients_controller.rb | 48 ++++++++++++++++ app/controllers/notes_controller.rb | 57 +++++++++++++++++++ app/helpers/application_helper.rb | 1 + app/models/client.rb | 3 + app/models/client_file.rb | 3 + app/models/note.rb | 3 + app/models/platform.rb | 3 + app/views/clients/_form.html.erb | 23 ++++++++ app/views/clients/edit.html.erb | 12 ++++ app/views/clients/index.html.erb | 29 ++++++++++ app/views/clients/new.html.erb | 11 ++++ app/views/layouts/application.html.erb | 6 +- app/views/notes/_form.html.erb | 24 ++++++++ app/views/notes/edit.html.erb | 12 ++++ app/views/notes/index.html.erb | 47 +++++++++++++++ app/views/notes/new.html.erb | 11 ++++ config/routes.rb | 2 + db/migrate/20250411170940_create_clients.rb | 11 ++++ db/migrate/20250411171000_create_platforms.rb | 12 ++++ db/migrate/20250411171038_create_notes.rb | 10 ++++ .../20250411171138_create_client_files.rb | 11 ++++ db/schema.rb | 51 ++++++++++++++--- test/fixtures/client_files.yml | 11 ++++ test/fixtures/clients.yml | 9 +++ test/fixtures/notes.yml | 9 +++ test/fixtures/platforms.yml | 13 +++++ test/models/client_file_test.rb | 7 +++ test/models/client_test.rb | 7 +++ test/models/note_test.rb | 7 +++ test/models/platform_test.rb | 7 +++ 30 files changed, 450 insertions(+), 10 deletions(-) create mode 100644 app/controllers/clients_controller.rb create mode 100644 app/controllers/notes_controller.rb create mode 100644 app/models/client.rb create mode 100644 app/models/client_file.rb create mode 100644 app/models/note.rb create mode 100644 app/models/platform.rb create mode 100644 app/views/clients/_form.html.erb create mode 100644 app/views/clients/edit.html.erb create mode 100644 app/views/clients/index.html.erb create mode 100644 app/views/clients/new.html.erb create mode 100644 app/views/notes/_form.html.erb create mode 100644 app/views/notes/edit.html.erb create mode 100644 app/views/notes/index.html.erb create mode 100644 app/views/notes/new.html.erb create mode 100644 db/migrate/20250411170940_create_clients.rb create mode 100644 db/migrate/20250411171000_create_platforms.rb create mode 100644 db/migrate/20250411171038_create_notes.rb create mode 100644 db/migrate/20250411171138_create_client_files.rb create mode 100644 test/fixtures/client_files.yml create mode 100644 test/fixtures/clients.yml create mode 100644 test/fixtures/notes.yml create mode 100644 test/fixtures/platforms.yml create mode 100644 test/models/client_file_test.rb create mode 100644 test/models/client_test.rb create mode 100644 test/models/note_test.rb create mode 100644 test/models/platform_test.rb diff --git a/app/controllers/clients_controller.rb b/app/controllers/clients_controller.rb new file mode 100644 index 0000000..99fc2da --- /dev/null +++ b/app/controllers/clients_controller.rb @@ -0,0 +1,48 @@ +class ClientsController < ApplicationController + + def index + @clients = Client.where(user_id: current_user.id) + end + + def new + @client = Client.new + end + + def create + @client = Client.new(client_params) + @client.user_id = current_user.id + if @client.save + redirect_to clients_path + else + render :new + end + end + + def edit + @client = Client.find(params[:id]) + end + + def update + @client = Client.find(params[:id]) + @client.user_id = current_user.id + if @client.update(client_params) + redirect_to clients_path + else + render 'edit' + end + end + + def destroy + @client = Client.find(params[:id]) + @client.destroy unless @client.blank? + + redirect_to clients_path + end + + private + + def client_params + params.require(:client).permit(:name, :description) + end + +end diff --git a/app/controllers/notes_controller.rb b/app/controllers/notes_controller.rb new file mode 100644 index 0000000..6491792 --- /dev/null +++ b/app/controllers/notes_controller.rb @@ -0,0 +1,57 @@ +class NotesController < ApplicationController + + def index + @client = Client.where(id: params[:client_id]).first + @notes = Note.where(client_id: params[:client_id]).order(name: :asc) + end + + def show + @note = Note.where('client_id = ? AND id = ?', params[:client_id], params[:id]) + end + + def new + @note = Note.new + @client = Client.where(id: params[:client_id]).first + end + + def create + @note = Note.new(note_params) + @note.client_id = params[:client_id] + @client = Client.where(id: params[:client_id]).first + if @note.save + redirect_to notes_path(client_id: @client.id) + else + render :new + end + end + + def edit + @note = Note.find(params[:id]) + @client = Client.where(id: params[:client_id]).first + end + + def update + @note = Note.find(params[:id]) + @client = Client.where(id: params[:client_id]).first + if @note.update(note_params) + redirect_to notes_path(client_id: @client.id) + else + render 'edit' + end + end + + def destroy + @note = Note.find(params[:id]) + @client = Client.where(id: params[:client_id]).first + @note.destroy unless @note.blank? + + redirect_to notes_path(client_id: @client.id) + end + + private + + def note_params + params.require(:note).permit(:name, :content) + end + +end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 0609e40..6c3b513 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -7,6 +7,7 @@ module ApplicationHelper ret += create_menu_li("glyphicon glyphicon-list-alt","Faktury","/invoices","invoices") ret += create_menu_li("glyphicon glyphicon-book","Klienci","/customers","customers") ret += create_menu_li("glyphicon glyphicon-shopping-cart","Produkty","/products","products") + ret += create_menu_li("glyphicon glyphicon-book","Obsługa","/clients","clients") ret += create_menu_li("glyphicon glyphicon-cog","Ustawienia","/settings/index","settings") return raw(ret) end diff --git a/app/models/client.rb b/app/models/client.rb new file mode 100644 index 0000000..f906397 --- /dev/null +++ b/app/models/client.rb @@ -0,0 +1,3 @@ +class Client < ApplicationRecord + belongs_to :user +end diff --git a/app/models/client_file.rb b/app/models/client_file.rb new file mode 100644 index 0000000..9e424e4 --- /dev/null +++ b/app/models/client_file.rb @@ -0,0 +1,3 @@ +class ClientFile < ApplicationRecord + belongs_to :client +end diff --git a/app/models/note.rb b/app/models/note.rb new file mode 100644 index 0000000..77dd21d --- /dev/null +++ b/app/models/note.rb @@ -0,0 +1,3 @@ +class Note < ApplicationRecord + belongs_to :client +end diff --git a/app/models/platform.rb b/app/models/platform.rb new file mode 100644 index 0000000..663b820 --- /dev/null +++ b/app/models/platform.rb @@ -0,0 +1,3 @@ +class Platform < ApplicationRecord + belongs_to :client +end diff --git a/app/views/clients/_form.html.erb b/app/views/clients/_form.html.erb new file mode 100644 index 0000000..8214fc0 --- /dev/null +++ b/app/views/clients/_form.html.erb @@ -0,0 +1,23 @@ + +<%= form_for @client, html: {class: "form-horizontal"} do |f| %> +<% if @client.errors.any? %> + <%= raw errors_to_html(@client.errors) %> +<% end %> +
+ <%= f.label :name, class: "col-sm-2 control-label" %> +
+ <%= f.text_field :name, class: "form-control", placeholder: 'Nazwa' %> +
+
+
+ <%= f.label :description, class: "col-sm-2 control-label" %> +
+ <%= f.text_area :description, class: "form-control" %> +
+
+
+
+ <%= f.submit 'Zapisz', class: "btn btn-primary" %> +
+
+<% end %> diff --git a/app/views/clients/edit.html.erb b/app/views/clients/edit.html.erb new file mode 100644 index 0000000..5e0c294 --- /dev/null +++ b/app/views/clients/edit.html.erb @@ -0,0 +1,12 @@ +
+
+
Edycja klienta
+
+ <%= link_to raw(" " + I18n.t('back')), clients_path, title: I18n.t('back'), class: "text-primary" %> +
+
+
+ <%= render 'form' %> +
+
+ diff --git a/app/views/clients/index.html.erb b/app/views/clients/index.html.erb new file mode 100644 index 0000000..36b126a --- /dev/null +++ b/app/views/clients/index.html.erb @@ -0,0 +1,29 @@ +
+
+
Klienci - obsługa
+ +
+ <%= link_to raw(' Nowy klient'), new_client_path, class: "text-primary" %>
+
+
+
+ + + + + + + + <% @clients.each do |client| %> + + + + + + <% end %> +
NazwaOpisAkcje
<%= link_to client.name, { controller: :notes, action: :index, client_id: client.id } %><%= client.description %> + <%= link_to raw(' Edycja'), edit_client_path(client), class: "btn btn-primary btn-xs" %> + <%= link_to raw(' Usuń'), client_path(client), class: "btn btn-danger btn-xs", method: :delete, data: { confirm: 'Czy na pewno usunąć?' } %> +
+
+
diff --git a/app/views/clients/new.html.erb b/app/views/clients/new.html.erb new file mode 100644 index 0000000..c1dc776 --- /dev/null +++ b/app/views/clients/new.html.erb @@ -0,0 +1,11 @@ +
+
+
Nowy klient
+
+ <%= link_to raw(" " + I18n.t('back')), clients_path, title: I18n.t('back'), class: "text-primary" %> +
+
+
+ <%= render 'form' %> +
+
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 09d4cba..eeb4d08 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -22,13 +22,13 @@ -
-
+
+
diff --git a/app/views/notes/index.html.erb b/app/views/notes/index.html.erb new file mode 100644 index 0000000..d719a3c --- /dev/null +++ b/app/views/notes/index.html.erb @@ -0,0 +1,47 @@ +
+
+
Klienci - notatki
+ +
+ <%= link_to raw(' Nowa notatka'), new_note_path(client_id: @client.id), class: "text-primary" %>
+
+
+
+ + + + + + + + <% @notes.each do |note| %> + + + + + + <% end %> +
NazwaZawartośćAkcje
<%= note.name %> + + <% short_content = truncate(note.content, length: 30, separator: ' ', omission: '...') %> + + +
+ <% if note.content.length > 30 %> + +
<%= raw simple_format(short_content) %>
+
+
+ <%= raw simple_format(note.content) %> +
+ <% else %> +
<%= raw simple_format(short_content) %>
+ <% end %> +
+ +
+ <%= link_to raw(' Edycja'), edit_note_path(note, client_id: @client.id), class: "btn btn-primary btn-xs" %> + <%= link_to raw(' Usuń'), note_path(note, client_id: @client.id), class: "btn btn-danger btn-xs", method: :delete, data: { confirm: 'Czy na pewno usunąć?' } %> +
+
+
diff --git a/app/views/notes/new.html.erb b/app/views/notes/new.html.erb new file mode 100644 index 0000000..e090646 --- /dev/null +++ b/app/views/notes/new.html.erb @@ -0,0 +1,11 @@ +
+
+
Nowa notatka
+
+ <%= link_to raw(" " + I18n.t('back')), notes_path(client_id: @client.id), title: I18n.t('back'), class: "text-primary" %> +
+
+
+ <%= render 'form' %> +
+
diff --git a/config/routes.rb b/config/routes.rb index 9c39fc8..74b2572 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,6 +6,8 @@ Rails.application.routes.draw do resources :products resources :invoices resources :customers + resources :clients + resources :notes root 'welcome#index' # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html diff --git a/db/migrate/20250411170940_create_clients.rb b/db/migrate/20250411170940_create_clients.rb new file mode 100644 index 0000000..0e4a7bb --- /dev/null +++ b/db/migrate/20250411170940_create_clients.rb @@ -0,0 +1,11 @@ +class CreateClients < ActiveRecord::Migration[5.0] + def change + create_table :clients do |t| + t.string :name + t.text :description + t.integer :user_id + + t.timestamps + end + end +end diff --git a/db/migrate/20250411171000_create_platforms.rb b/db/migrate/20250411171000_create_platforms.rb new file mode 100644 index 0000000..8f329d3 --- /dev/null +++ b/db/migrate/20250411171000_create_platforms.rb @@ -0,0 +1,12 @@ +class CreatePlatforms < ActiveRecord::Migration[5.0] + def change + create_table :platforms do |t| + t.string :name + t.string :url + t.text :login_info + t.references :client, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/migrate/20250411171038_create_notes.rb b/db/migrate/20250411171038_create_notes.rb new file mode 100644 index 0000000..bfc940a --- /dev/null +++ b/db/migrate/20250411171038_create_notes.rb @@ -0,0 +1,10 @@ +class CreateNotes < ActiveRecord::Migration[5.0] + def change + create_table :notes do |t| + t.text :content + t.references :client, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/migrate/20250411171138_create_client_files.rb b/db/migrate/20250411171138_create_client_files.rb new file mode 100644 index 0000000..e530170 --- /dev/null +++ b/db/migrate/20250411171138_create_client_files.rb @@ -0,0 +1,11 @@ +class CreateClientFiles < ActiveRecord::Migration[5.0] + def change + create_table :client_files do |t| + t.string :name + t.text :description + t.references :client, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index c09f18e..2cd40c9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,23 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20161004200042) do +ActiveRecord::Schema.define(version: 20250411171138) do + + create_table "client_files", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci" do |t| + t.string "name" + t.text "description", limit: 65535 + t.integer "client_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["client_id"], name: "index_client_files_on_client_id", using: :btree + end + + create_table "clients", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci" do |t| + t.string "name" + t.text "description", limit: 65535 + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end create_table "customers", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci" do |t| t.string "name" @@ -27,9 +43,9 @@ ActiveRecord::Schema.define(version: 20161004200042) do create_table "invoice_products", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci" do |t| t.integer "invoice_id" - t.integer "product_id" - t.decimal "netto_price", precision: 18, scale: 2 - t.decimal "qty", precision: 18, scale: 3 + t.integer "product_id", null: false + t.decimal "netto_price", precision: 10, scale: 2, null: false + t.decimal "qty", precision: 10, scale: 3, null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["invoice_id"], name: "index_invoice_products_on_invoice_id", using: :btree @@ -50,6 +66,24 @@ ActiveRecord::Schema.define(version: 20161004200042) do t.index ["user_id"], name: "index_invoices_on_user_id", using: :btree end + create_table "notes", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci" do |t| + t.text "content", limit: 65535 + t.integer "client_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["client_id"], name: "index_notes_on_client_id", using: :btree + end + + create_table "platforms", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci" do |t| + t.string "name" + t.string "url" + t.text "login_info", limit: 65535 + t.integer "client_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["client_id"], name: "index_platforms_on_client_id", using: :btree + end + create_table "products", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci" do |t| t.string "name" t.integer "vat_id" @@ -73,9 +107,9 @@ ActiveRecord::Schema.define(version: 20161004200042) do t.string "bank_name" t.string "bank_account" t.boolean "main" - t.boolean "active" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.boolean "active", default: true, null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.index ["user_id"], name: "index_user_firms_on_user_id", using: :btree end @@ -104,12 +138,15 @@ ActiveRecord::Schema.define(version: 20161004200042) do t.datetime "updated_at", null: false end + add_foreign_key "client_files", "clients" add_foreign_key "customers", "users" add_foreign_key "invoice_products", "invoices" add_foreign_key "invoice_products", "products" add_foreign_key "invoices", "customers" add_foreign_key "invoices", "user_firms" add_foreign_key "invoices", "users" + add_foreign_key "notes", "clients" + add_foreign_key "platforms", "clients" add_foreign_key "products", "users" add_foreign_key "products", "vats" add_foreign_key "user_firms", "users" diff --git a/test/fixtures/client_files.yml b/test/fixtures/client_files.yml new file mode 100644 index 0000000..a5b3e50 --- /dev/null +++ b/test/fixtures/client_files.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + name: MyString + description: MyText + client: one + +two: + name: MyString + description: MyText + client: two diff --git a/test/fixtures/clients.yml b/test/fixtures/clients.yml new file mode 100644 index 0000000..2ff75a8 --- /dev/null +++ b/test/fixtures/clients.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + name: MyString + description: MyText + +two: + name: MyString + description: MyText diff --git a/test/fixtures/notes.yml b/test/fixtures/notes.yml new file mode 100644 index 0000000..b7569a5 --- /dev/null +++ b/test/fixtures/notes.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + content: MyText + client: one + +two: + content: MyText + client: two diff --git a/test/fixtures/platforms.yml b/test/fixtures/platforms.yml new file mode 100644 index 0000000..66b6f07 --- /dev/null +++ b/test/fixtures/platforms.yml @@ -0,0 +1,13 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + name: MyString + url: MyString + login_info: MyText + client: one + +two: + name: MyString + url: MyString + login_info: MyText + client: two diff --git a/test/models/client_file_test.rb b/test/models/client_file_test.rb new file mode 100644 index 0000000..ab0bddd --- /dev/null +++ b/test/models/client_file_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ClientFileTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/client_test.rb b/test/models/client_test.rb new file mode 100644 index 0000000..2dfc9cc --- /dev/null +++ b/test/models/client_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ClientTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/note_test.rb b/test/models/note_test.rb new file mode 100644 index 0000000..7bbab53 --- /dev/null +++ b/test/models/note_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class NoteTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/platform_test.rb b/test/models/platform_test.rb new file mode 100644 index 0000000..3a68ca9 --- /dev/null +++ b/test/models/platform_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class PlatformTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end