diff --git a/app/controllers/grants_controller.rb b/app/controllers/grants_controller.rb index 28f828a..730198e 100644 --- a/app/controllers/grants_controller.rb +++ b/app/controllers/grants_controller.rb @@ -96,6 +96,7 @@ class GrantsController < ApplicationController @experts = Expert.all @expenses = Expense.all @partners = Partner.all + @locations = Location.where.not(id: 1) end # Only allow a list of trusted parameters through. @@ -112,6 +113,6 @@ class GrantsController < ApplicationController :start_date_month, :end_date_month, :ann_date_month, :start_date_always, :end_date_always, :ann_date_always, project_ids: [], tag_ids: [], company_activity_ids: [], - company_size_ids: [], expense_ids: []) + company_size_ids: [], expense_ids: [], location_ids: []) end end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 9b1003f..f632dc0 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -170,19 +170,20 @@ class HomeController < ApplicationController @company_size_chk = params[:company_size] || [] @company_activity_chk = params[:company_activity] || [] @project_chk = params[:project] || [] + @location_chk = params[:location] || [] @company_sizes = CompanySize.all @company_activities = CompanyActivity.all @projects = Project.all + @locations = Location.where.not(id: 1) end - def pdf_header - - end + def pdf_header; end def build_filter_hash { search: params[:search], company_sizes: @company_size_chk, company_activities: @company_activity_chk, projects: @project_chk, + locations: @location_chk, localization: params[:localization], ammount_chk: params[:ammount_chk], ammount_price: params[:ammount_price] } diff --git a/app/controllers/test_controller.rb b/app/controllers/test_controller.rb new file mode 100644 index 0000000..f198f9d --- /dev/null +++ b/app/controllers/test_controller.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +# Test controller only admin access +class TestController < ApplicationController + def add_locations + return unless Location.all.blank? + + Location.create(parent_id: nil, name: 'Cała polska') + Location.create(parent_id: 1, name: 'dolnośląskie') + Location.create(parent_id: 1, name: 'kujawsko-pomorskie') + Location.create(parent_id: 1, name: 'lubelskie') + Location.create(parent_id: 1, name: 'lubuskie') + Location.create(parent_id: 1, name: 'łódzkie') + Location.create(parent_id: 1, name: 'małopolskie') + Location.create(parent_id: 1, name: 'mazowieckie') + Location.create(parent_id: 1, name: 'opolskie') + Location.create(parent_id: 1, name: 'podkarpackie') + Location.create(parent_id: 1, name: 'podlaskie') + Location.create(parent_id: 1, name: 'pomorskie') + Location.create(parent_id: 1, name: 'śląskie') + Location.create(parent_id: 1, name: 'świętokrzyskie') + Location.create(parent_id: 1, name: 'warmińsko-mazurskie') + Location.create(parent_id: 1, name: 'wielkopolskie') + Location.create(parent_id: 1, name: 'zachodniopomorskie') + end +end diff --git a/app/models/dotation.rb b/app/models/dotation.rb index e79cfe5..416264b 100644 --- a/app/models/dotation.rb +++ b/app/models/dotation.rb @@ -36,6 +36,7 @@ class Dotation < ApplicationRecord has_and_belongs_to_many :company_sizes has_and_belongs_to_many :projects has_and_belongs_to_many :tags + has_and_belongs_to_many :locations has_and_belongs_to_many :company_activities # == Validations ========================================================== validates :name, presence: true, length: { maximum: 255 } @@ -61,6 +62,9 @@ class Dotation < ApplicationRecord scope :by_comp_size, (lambda do |val| joins(:company_sizes).merge(CompanySize.by_ids(val)).distinct end) + scope :by_locations, (lambda do |val| + joins(:locations).merge(Location.by_ids(val)).distinct + end) scope :by_comp_active, (lambda do |val| joins(:company_activities).merge(CompanyActivity.by_ids(val)).distinct end) @@ -92,6 +96,9 @@ class Dotation < ApplicationRecord unless filters[:company_sizes].blank? ret = ret.by_comp_size(filters[:company_sizes]) end + unless filters[:locations].blank? + ret = ret.by_locations(filters[:locations]) + end unless filters[:company_activities].blank? ret = ret.by_comp_active(filters[:company_activities]) end @@ -178,4 +185,18 @@ class Dotation < ApplicationRecord end doc end + + def location_to_text + ret = '' + if locations.size == 16 + ret = 'Cała polska' + else + l_size = locations.size + locations.each_with_index do |loca, index| + ret += loca.name + ret += ', ' if l_size > index + 1 + end + end + ret + end end diff --git a/app/models/location.rb b/app/models/location.rb new file mode 100644 index 0000000..0c35905 --- /dev/null +++ b/app/models/location.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +class Location < ApplicationRecord + # == Constants ============================================================ + + # == Attributes =========================================================== + + # == Extensions =========================================================== + + # == Relationships ======================================================== + has_and_belongs_to_many :dotations + # == Validations ========================================================== + + # == Scopes =============================================================== + scope :by_ids, ->(val) { where(id: val) } + # == Callbacks ============================================================ + + # == Class Methods ======================================================== + + # == Instance Methods ===================================================== +end diff --git a/app/views/grants/_form.html.erb b/app/views/grants/_form.html.erb index 5877d87..9a2d646 100644 --- a/app/views/grants/_form.html.erb +++ b/app/views/grants/_form.html.erb @@ -113,10 +113,16 @@
Find me in app/views/test/add_locations.html.erb
diff --git a/config/locales/pl/models/dotation.yml b/config/locales/pl/models/dotation.yml index 67025e9..df7df5f 100644 --- a/config/locales/pl/models/dotation.yml +++ b/config/locales/pl/models/dotation.yml @@ -21,6 +21,7 @@ pl: max_percent: Maksymalny poziom dofinansowania company_activities: Dominująca działalność firmy company_sizes: Wielkość firmy + locations: Województwa projects: Rodzaj przedsięwzięcia expert: Ekspert expert_id: Ekspert diff --git a/config/routes.rb b/config/routes.rb index 9b6052b..bfe4e2d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + get 'test/add_locations' get 'dashboard/index' get 'zrezygnuj' => 'email_filter#unsubscribe' get 'email_filter/unsubscribe' diff --git a/db/migrate/20220829070257_create_locations.rb b/db/migrate/20220829070257_create_locations.rb new file mode 100644 index 0000000..ae5efe6 --- /dev/null +++ b/db/migrate/20220829070257_create_locations.rb @@ -0,0 +1,10 @@ +class CreateLocations < ActiveRecord::Migration[5.2] + def change + create_table :locations do |t| + t.bigint :parent_id + t.string :name + + t.timestamps + end + end +end diff --git a/db/migrate/20220830110315_create_join_table_dotation_location.rb b/db/migrate/20220830110315_create_join_table_dotation_location.rb new file mode 100644 index 0000000..69a59c3 --- /dev/null +++ b/db/migrate/20220830110315_create_join_table_dotation_location.rb @@ -0,0 +1,8 @@ +class CreateJoinTableDotationLocation < ActiveRecord::Migration[5.2] + def change + create_join_table :dotations, :locations do |t| + # t.index [:dotation_id, :location_id] + # t.index [:location_id, :dotation_id] + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 02ebd8b..73c0aed 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.define(version: 2022_07_31_134927) do +ActiveRecord::Schema.define(version: 2022_08_30_110315) do create_table "active_storage_attachments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "name", null: false @@ -152,6 +152,11 @@ ActiveRecord::Schema.define(version: 2022_07_31_134927) do t.index ["dotation_id", "expense_id"], name: "dot_expind" end + create_table "dotations_locations", id: false, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + t.bigint "dotation_id", null: false + t.bigint "location_id", null: false + end + create_table "dotations_projects", id: false, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.bigint "dotation_id", null: false t.bigint "project_id", null: false @@ -212,6 +217,13 @@ ActiveRecord::Schema.define(version: 2022_07_31_134927) do t.index ["sluggable_type", "sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_type_and_sluggable_id" end + create_table "locations", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + t.bigint "parent_id" + t.string "name" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "partners", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "name" t.text "description" diff --git a/test/fixtures/locations.yml b/test/fixtures/locations.yml new file mode 100644 index 0000000..4d4d11b --- /dev/null +++ b/test/fixtures/locations.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + parent_id: + name: MyString + +two: + parent_id: + name: MyString diff --git a/test/models/location_test.rb b/test/models/location_test.rb new file mode 100644 index 0000000..cd10e31 --- /dev/null +++ b/test/models/location_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class LocationTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end