diff --git a/app/assets/javascripts/form_calendar.js b/app/assets/javascripts/form_calendar.js new file mode 100644 index 0000000..b2aaea1 --- /dev/null +++ b/app/assets/javascripts/form_calendar.js @@ -0,0 +1,23 @@ +$(function () { + $('.select2').select2(); + $("input[data-bootstrap-switch]").each(function(){ + $(this).bootstrapSwitch(); + }) + $( "#search_input" ).on('input', function() { + if ($( "#search_input" ).val().length > 2 || $( "#search_input" ).val().length == 0) { + $('#search').val($( "#search_input" ).val()); + $('#submit_form_btn').click(); + } + }); +}); +function formSubmit() { + $('#submit_form_btn').click(); + return false; +} +function formSubmitText(element) { + alert(this); + if ($(element).val().length > 2 || $(element).val().length == 0) { + //$('#submit_form_btn').click(); + } + return false; +} diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index d0d340f..0427664 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -10,6 +10,12 @@ class HomeController < ApplicationController .point_desc.end_date_asc.page(params[:page]) end + def search + prepare_filters + @dotations = Dotation.search_with_filters(build_filter_hash).public_dot + .point_desc.end_date_asc.page(params[:page]) + end + def contact; end def show @@ -20,8 +26,20 @@ class HomeController < ApplicationController private def prepare_filters + @company_size_chk = params[:company_size] || [] + @company_activity_chk = params[:company_activity] || [] + @project_chk = params[:project] || [] @company_sizes = CompanySize.all @company_activities = CompanyActivity.all @projects = Project.all end + + def build_filter_hash + { + search: params[:search], company_sizes: @company_size_chk, + company_activities: @company_activity_chk, projects: @project_chk, + localization: params[:localization], ammount_chk: params[:ammount_chk], + ammount_price: params[:ammount_price] + } + end end diff --git a/app/models/company_activity.rb b/app/models/company_activity.rb index ecad7af..8bf81f9 100644 --- a/app/models/company_activity.rb +++ b/app/models/company_activity.rb @@ -13,7 +13,7 @@ class CompanyActivity < ApplicationRecord # == Validations ========================================================== validates :name, presence: true # == Scopes =============================================================== - + scope :by_ids, ->(val) { where(id: val) } # == Callbacks ============================================================ # == Class Methods ======================================================== diff --git a/app/models/company_size.rb b/app/models/company_size.rb index ac11a5e..d1b732e 100644 --- a/app/models/company_size.rb +++ b/app/models/company_size.rb @@ -13,7 +13,7 @@ class CompanySize < ApplicationRecord # == Validations ========================================================== validates :name, presence: true # == Scopes =============================================================== - + scope :by_ids, ->(val) { where(id: val) } # == Callbacks ============================================================ # == Class Methods ======================================================== diff --git a/app/models/dotation.rb b/app/models/dotation.rb index 3cd7bf0..597c6da 100644 --- a/app/models/dotation.rb +++ b/app/models/dotation.rb @@ -3,7 +3,17 @@ # Dotations class Dotation < ApplicationRecord # == Constants ============================================================ + AMMOUNT_ON_PROJECT = [ + [0, 'poniżej 100 tyś zł', 0, 99_999], + [1, '100 tyś do 1 mln zł', 100_000, 999_999], + [2, '1 mln do 10 mln zł', 1_000_000, 10_000_000], + [3, 'powyżej 10 mln zł', 10_000_000, 999_999_999_999_999] + ].freeze + AMMOUNT_ON_PROJECT_HASH = { + '' => '', 'poniżej 100 tyś zł' => 0, '100 tyś do 1 mln zł' => 1, + '1 mln do 10 mln zł' => 2, 'powyżej 10 mln zł' => 3 + }.freeze # == Attributes =========================================================== # == Extensions =========================================================== @@ -29,15 +39,55 @@ class Dotation < ApplicationRecord value: "%#{search_value}%") end) scope :extra_search, (lambda do |search_value| - where('name LIKE :value OR formal_name LIKE :value', + where('dotations.name LIKE :value OR dotations.formal_name LIKE :value OR '\ + 'dotations.full_descr LIKE'\ + ' :value OR dotations.positioning_text LIKE :value', value: "%#{search_value}%") end) + scope :by_localization, (lambda do |search_value| + where('dotations.localization LIKE :value', value: "%#{search_value}%") + end) + scope :by_prize, (lambda do |val_from, val_to| + where('dotations.max_amount > :val_f AND dotations.max_amount < :val_t', + val_f: val_from, val_t: val_to) + end) + scope :by_comp_size, (lambda do |val| + joins(:company_sizes).merge(CompanySize.by_ids(val)).distinct + end) + scope :by_comp_active, (lambda do |val| + joins(:company_activities).merge(CompanyActivity.by_ids(val)).distinct + end) + scope :by_projects, (lambda do |val| + joins(:projects).merge(Project.by_ids(val)).distinct + end) + scope :min_prize, ->(val) { where('dotations.min_amount < ?', val) } scope :point_desc, -> { order(points: :desc) } scope :end_date_asc, -> { order(end_date: :asc) } scope :public_dot, -> { where(active: true) } # == Callbacks ============================================================ # == Class Methods ======================================================== - + def self.search_with_filters(filters) + ret = Dotation.extra_search(filters[:search]) + ret = ret.by_localization(filters[:localization]) if filters[:localization] + # ammount + if filters[:ammount_chk].blank? && !filters[:ammount_price].blank? + ret = ret.min_prize(filters[:ammount_price]) + elsif !filters[:ammount_chk].blank? + ret = + ret.by_prize( + Dotation::AMMOUNT_ON_PROJECT[filters[:ammount_chk].to_i][2], + Dotation::AMMOUNT_ON_PROJECT[filters[:ammount_chk].to_i][3] + ) + end + unless filters[:company_sizes].blank? + ret = ret.by_comp_size(filters[:company_sizes]) + end + unless filters[:company_activities].blank? + ret = ret.by_comp_active(filters[:company_activities]) + end + ret = ret.by_projects(filters[:projects]) unless filters[:projects].blank? + ret + end # == Instance Methods ===================================================== end diff --git a/app/models/project.rb b/app/models/project.rb index 96ee725..a337695 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -13,7 +13,7 @@ class Project < ApplicationRecord # == Validations ========================================================== validates :name, presence: true # == Scopes =============================================================== - + scope :by_ids, ->(val) { where(id: val) } # == Callbacks ============================================================ # == Class Methods ======================================================== diff --git a/app/views/home/_dotations.html.erb b/app/views/home/_dotations.html.erb index 5590818..4080fda 100644 --- a/app/views/home/_dotations.html.erb +++ b/app/views/home/_dotations.html.erb @@ -1,16 +1,26 @@ <% if @dotations.blank? %> -
-
-

Uwagi

+
+
+ <%= render 'filter_form' %>
-
-
- -
Informacja
- Brak danych do wyświetlenia +
+
+
+

Uwagi

+
+
+
+ +
Informacja
+ Brak danych do wyświetlenia +
+
+
+
+
<% else %> diff --git a/app/views/home/_filter_form.html.erb b/app/views/home/_filter_form.html.erb index 7e99311..940de62 100644 --- a/app/views/home/_filter_form.html.erb +++ b/app/views/home/_filter_form.html.erb @@ -1,10 +1,9 @@ -<% if params[:search] %> - - +<%= form_tag(home_search_path, method: :get, remote: true, id: 'filter_form') do %>
-

Filtry

+

Aktywne filtry

+

<%= @dotations.count %>

@@ -12,6 +11,7 @@

Wyszukiwana fraza:
<%= params[:search] %>

+ <%= hidden_field_tag :search, params[:search] %>
@@ -30,7 +30,7 @@
<% @company_sizes.each do |company_size| %>
- + <%= check_box_tag("company_size[]", company_size.id, @company_size_chk.include?(company_size.id.to_s) , class: 'form-check-input', onchange: "formSubmit()") %>
<% end %> @@ -41,19 +41,19 @@
-
+
<% @company_activities.each do |company_activity| %> -
- - -
+
+ <%= check_box_tag("company_activity[]", company_activity.id, @company_activity_chk.include?(company_activity.id.to_s) , class: 'form-check-input', onchange: "formSubmit()") %> + +
<% end %>
@@ -62,27 +62,74 @@
-
+
<% @projects.each do |project| %> -
- - -
+
+ <%= check_box_tag("project[]", project.id, @project_chk.include?(project.id.to_s) , class: 'form-check-input', onchange: "formSubmit()") %> + +
<% end %>
+
+ +
+
+
+ + <%= text_field_tag(:localization, params[:localization], class: 'form-control', placeholder: 'Wpisz tu lokalizację') %> +
+
+
+
+
+ +
+
+
+ + <%= number_field_tag(:ammount_price, params[:ammount_price], class: 'form-control', placeholder: 'Wpisz tu wartość') %> +
+
+ + <%= select_tag "ammount_chk", options_for_select(Dotation::AMMOUNT_ON_PROJECT_HASH, params[:ammount_chk]), class: 'form-control', onchange: "formSubmit()" %> +
+
+
+ +
+
+
+ +
+
+
- <%= form_tag(home_index_path, method: :get, remote: true) do %>
- <%= text_field_tag(:search, params[:search], class: 'form-control form-control-lg', placeholder: 'Wpisz wyszukiwaną frazę') %> + <%= text_field_tag(:search, params[:search], class: 'form-control form-control-lg', placeholder: 'Wpisz wyszukiwaną frazę', id: 'search_input') %>
- <% end %>

diff --git a/app/views/home/search.js.erb b/app/views/home/search.js.erb new file mode 100644 index 0000000..9f2e024 --- /dev/null +++ b/app/views/home/search.js.erb @@ -0,0 +1,2 @@ +$('#search_result').html("<%= escape_javascript(render partial: 'dotations') %>"); +reloadFunctionsOnAjax(); diff --git a/app/views/layouts/home_layout.html.erb b/app/views/layouts/home_layout.html.erb index f68a85d..049c6f3 100644 --- a/app/views/layouts/home_layout.html.erb +++ b/app/views/layouts/home_layout.html.erb @@ -41,6 +41,9 @@
-<%= javascript_include_tag 'jquery.min', 'bootstrap.bundle.min', 'bs-custom-file-input.min', 'moment-with-locales.min', 'select2.full.min', 'tempusdominus-bootstrap-4.min', 'adminlte.min', 'bootstrap-switch.min', 'forms' %> +<%= javascript_include_tag 'jquery.min', 'bootstrap.bundle.min', 'bs-custom-file-input.min', 'moment-with-locales.min', 'select2.full.min', 'tempusdominus-bootstrap-4.min', 'adminlte.min', 'bootstrap-switch.min', 'form_calendar' %> + diff --git a/config/routes.rb b/config/routes.rb index 77f9d02..25367ee 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,8 @@ Rails.application.routes.draw do get 'kontakt' => 'home#contact' get 'dotacja/:id' => 'home#show' + get 'home/search' + post 'home/search' resources :home, only: %i[index show] devise_for :users resources :company_sizes diff --git a/db/migrate/20220223174458_create_dotations.rb b/db/migrate/20220223174458_create_dotations.rb index e74d4cb..22f6e6d 100644 --- a/db/migrate/20220223174458_create_dotations.rb +++ b/db/migrate/20220223174458_create_dotations.rb @@ -5,9 +5,9 @@ class CreateDotations < ActiveRecord::Migration[5.2] t.string :formal_name t.date :date_from t.date :date_to - t.integer :min_amount + t.bigint :min_amount t.integer :min_amount_curr_id, default: 1 - t.integer :max_amount + t.bigint :max_amount t.integer :max_amount_curr_id, default: 1 t.integer :max_percent t.text :localization