From 12d9ffc36259c8b7f6444e706e52551129c7d3ec Mon Sep 17 00:00:00 2001 From: Behnam Ahmad Khan Beigi Date: Mon, 13 Feb 2017 19:53:28 +0330 Subject: [PATCH 1/5] base structure of objects filter created for index section --- .../dashboard/sections/resources_index.rb | 83 +++++++++++-------- 1 file changed, 47 insertions(+), 36 deletions(-) diff --git a/lib/faalis/dashboard/sections/resources_index.rb b/lib/faalis/dashboard/sections/resources_index.rb index 05649288..c27656fa 100644 --- a/lib/faalis/dashboard/sections/resources_index.rb +++ b/lib/faalis/dashboard/sections/resources_index.rb @@ -20,57 +20,68 @@ def index protected - # Fetch all or part of the corresponding resource - # from data base with respect to `scope` DSL. - # - # The important thing here is that by using `scope` - # DSL this method will chain the resulted scope - # with other scopes like `page` and `policy_scope` - def fetch_index_objects + # Fetch all or part of the corresponding resource + # from data base with respect to `scope` DSL. + # + # The important thing here is that by using `scope` + # DSL this method will chain the resulted scope + # with other scopes like `page` and `policy_scope` + def fetch_index_all_objects scope = index_properties.default_scope - if !scope.nil? - - # If user provided an scope for `index` section. + if !scope.nil? - if scope.respond_to? :call - # If scope provided by a block - scope = scope.call - else - # If scope provided by a symbol - # which should be a method name - scope = self.send(scope) - end + # If user provided an scope for `index` section. + if scope.respond_to? :call + # If scope provided by a block + scope = scope.call else - scope = model.all - #scope = ApplicationPolicy::Scope.new(current_user, model.all).resolve + # If scope provided by a symbol + # which should be a method name + scope = self.send(scope) end - scope = scope.order('created_at DESC').page(params[:page]) - policy_scope(scope) - + else + scope = model.all + #scope = ApplicationPolicy::Scope.new(current_user, model.all).resolve end - def index_properties - Faalis::Dashboard::DSL::Index.new(model) - end + scope = scope.order('created_at DESC').page(params[:page]) + policy_scope(scope) - private + end + + def fetch_index_filtered_objects + #strong.each do |k, v| + # q.where(k.to_sym => v) + #end + end + + def index_properties + Faalis::Dashboard::DSL::Index.new(model) + end - def fetch_and_set_all - result = fetch_index_objects - instance_variable_set("@#{plural_name}", result) + private - @index_fields = index_properties.fields - @resources = result + def fetch_and_set_all + # rename ---- fetch_all_index_objects + if params.has_key? :filter + result = fetch_index_filtered_objects + else + result = fetch_index_all_objects end + instance_variable_set("@#{plural_name}", result) - # You can override this method to change the behaviour of `index` - # action - def index_hook(resources) + @index_fields = index_properties.fields + @resources = result + end - end + # You can override this method to change the behaviour of `index` + # action + def index_hook(resources) + + end # The actual DSL for index ages module ClassMethods From aa18c17dd381297bf9b0c7804ba5004b8c627720 Mon Sep 17 00:00:00 2001 From: Behnam Ahmad Khan Beigi Date: Tue, 14 Feb 2017 17:10:51 +0330 Subject: [PATCH 2/5] filter by query string completed in index section --- .../dashboard/sections/resources_index.rb | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/faalis/dashboard/sections/resources_index.rb b/lib/faalis/dashboard/sections/resources_index.rb index c27656fa..5cc9d508 100644 --- a/lib/faalis/dashboard/sections/resources_index.rb +++ b/lib/faalis/dashboard/sections/resources_index.rb @@ -52,10 +52,12 @@ def fetch_index_all_objects end - def fetch_index_filtered_objects - #strong.each do |k, v| - # q.where(k.to_sym => v) - #end + def fetch_index_filtered_objects(filter_keys) + items = fetch_index_all_objects + filter_keys.each do |v| + items = items.where(v => params[v]) + end + items end def index_properties @@ -66,11 +68,21 @@ def index_properties def fetch_and_set_all # rename ---- fetch_all_index_objects - if params.has_key? :filter - result = fetch_index_filtered_objects - else + params_keys = Set.new(params).map(&:to_sym) + fields = model.columns_hash.keys.map(&:to_sym) + filter_keys = params_keys & fields + + # Check if the params have common with model fields + # pass it to the `fetch_index_filtered_objects` to + # filtering the query. it used for nested sections + # and search result + + if filter_keys.empty? result = fetch_index_all_objects + else + result = fetch_index_filtered_objects(filter_keys) end + instance_variable_set("@#{plural_name}", result) @index_fields = index_properties.fields From 8c4242fc57dcb5b27395206acf70364ba3a386a7 Mon Sep 17 00:00:00 2001 From: Behnam Ahmad Khan Beigi Date: Thu, 23 Feb 2017 21:48:01 +0330 Subject: [PATCH 3/5] show resource temporary modified commited for review --- app/views/faalis/dashboard/resource/show.html.slim | 5 +++++ lib/faalis/dashboard/sections/resource_show.rb | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/app/views/faalis/dashboard/resource/show.html.slim b/app/views/faalis/dashboard/resource/show.html.slim index 8a7a9e3d..745d4a64 100644 --- a/app/views/faalis/dashboard/resource/show.html.slim +++ b/app/views/faalis/dashboard/resource/show.html.slim @@ -1,3 +1,8 @@ +.nav-tabs-custom + ul.nav.nav-tabs +- @reflection_models.each do |mdl| + li = link_to mdl + .row .col-sm-12.col-lg-12 .box.box-primary diff --git a/lib/faalis/dashboard/sections/resource_show.rb b/lib/faalis/dashboard/sections/resource_show.rb index a16ae975..90079b25 100644 --- a/lib/faalis/dashboard/sections/resource_show.rb +++ b/lib/faalis/dashboard/sections/resource_show.rb @@ -13,6 +13,10 @@ def show collect_model_fields_for_show @resource_title = t(_resource_title.singularize) + @reflection_models = model.reflect_on_all_associations(:has_many). + map(&:klass) + + byebug show_hook(@resource) From 53d78fe1ad5a83f1cd09667476d85fb374e94f29 Mon Sep 17 00:00:00 2001 From: Behnam Ahmad Khan Beigi Date: Mon, 27 Feb 2017 20:32:48 +0330 Subject: [PATCH 4/5] reflection links added to dashboard --- .../faalis/dashboard/resource/show.html.slim | 4 ++-- lib/faalis/dashboard/sections/resource_show.rb | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app/views/faalis/dashboard/resource/show.html.slim b/app/views/faalis/dashboard/resource/show.html.slim index 745d4a64..c953d7a3 100644 --- a/app/views/faalis/dashboard/resource/show.html.slim +++ b/app/views/faalis/dashboard/resource/show.html.slim @@ -1,7 +1,7 @@ .nav-tabs-custom ul.nav.nav-tabs -- @reflection_models.each do |mdl| - li = link_to mdl + - @reflection_controller.each do |mdl| + li = link_to(t(mdl), controller: mdl, "#{controller_name.singularize}_id" => @resource.id) .row .col-sm-12.col-lg-12 diff --git a/lib/faalis/dashboard/sections/resource_show.rb b/lib/faalis/dashboard/sections/resource_show.rb index 90079b25..f5474022 100644 --- a/lib/faalis/dashboard/sections/resource_show.rb +++ b/lib/faalis/dashboard/sections/resource_show.rb @@ -13,10 +13,19 @@ def show collect_model_fields_for_show @resource_title = t(_resource_title.singularize) - @reflection_models = model.reflect_on_all_associations(:has_many). - map(&:klass) - byebug + # list all the model names that have :has_many relation + reflection_models = model.reflect_on_all_associations(:has_many). + map(&:class_name) + + @reflection_controller = [] + + if reflection_models.length > 0 + reflection_models.each do |mdl| + @reflection_controller.push mdl.tableize.split('/')[-1] + end + end + show_hook(@resource) From bb40947f118e99356902e3f65dd4036c865b247f Mon Sep 17 00:00:00 2001 From: Behnam Ahmad Khan Beigi Date: Mon, 3 Apr 2017 15:52:21 +0430 Subject: [PATCH 5/5] filter object keys added to section resources --- config/locales/faalis.en.yml | 4 +++- lib/faalis/dashboard/sections/resource.rb | 13 +++++++++++-- lib/faalis/dashboard/sections/resource_create.rb | 1 - lib/faalis/dashboard/sections/resources_index.rb | 11 ++++------- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/config/locales/faalis.en.yml b/config/locales/faalis.en.yml index 521b375e..f5d2b996 100644 --- a/config/locales/faalis.en.yml +++ b/config/locales/faalis.en.yml @@ -1,5 +1,7 @@ --- en: + dashboard: Dashboard + index: index faalis: confirm_password: Password Confirmation forgot_your_password: Forget your password @@ -58,7 +60,7 @@ en: permission: permissions: Permissions - + dashboard: users: Users groups: Groups diff --git a/lib/faalis/dashboard/sections/resource.rb b/lib/faalis/dashboard/sections/resource.rb index a9480a7a..ea79afd4 100644 --- a/lib/faalis/dashboard/sections/resource.rb +++ b/lib/faalis/dashboard/sections/resource.rb @@ -68,6 +68,12 @@ def _engine private + def filter_params + params_keys = Set.new(params).map(&:to_sym) + fields = model.columns_hash.keys.map(&:to_sym) + params_keys & fields + end + def _route_name nil end @@ -101,15 +107,18 @@ def _show_route(id) def _edit_route(id) if respond_to? :edit + filter_params_hash = Hash[filter_params.map{|key| [key, params[key]]}] url_for(controller: params[:controller], action: :edit, - id: id) + id: id, + **filter_params_hash) end end def _new_route if respond_to? :new - url_for(controller: params[:controller], action: :new) + filter_params_hash = Hash[filter_params.map{|key| [key, params[key]]}] + url_for(controller: params[:controller], action: :new, **filter_params_hash) end end diff --git a/lib/faalis/dashboard/sections/resource_create.rb b/lib/faalis/dashboard/sections/resource_create.rb index f2c86122..4273c7dc 100644 --- a/lib/faalis/dashboard/sections/resource_create.rb +++ b/lib/faalis/dashboard/sections/resource_create.rb @@ -70,7 +70,6 @@ def update # The actual action method for creating new resource. def create authorize model - @resource = model.new(creation_params) @resource.assign_attributes(**reflections_hash) unless reflections_hash.nil? diff --git a/lib/faalis/dashboard/sections/resources_index.rb b/lib/faalis/dashboard/sections/resources_index.rb index 5cc9d508..67134250 100644 --- a/lib/faalis/dashboard/sections/resources_index.rb +++ b/lib/faalis/dashboard/sections/resources_index.rb @@ -52,9 +52,9 @@ def fetch_index_all_objects end - def fetch_index_filtered_objects(filter_keys) + def fetch_index_filtered_objects(filter_params) items = fetch_index_all_objects - filter_keys.each do |v| + filter_params.each do |v| items = items.where(v => params[v]) end items @@ -68,19 +68,16 @@ def index_properties def fetch_and_set_all # rename ---- fetch_all_index_objects - params_keys = Set.new(params).map(&:to_sym) - fields = model.columns_hash.keys.map(&:to_sym) - filter_keys = params_keys & fields # Check if the params have common with model fields # pass it to the `fetch_index_filtered_objects` to # filtering the query. it used for nested sections # and search result - if filter_keys.empty? + if filter_params.empty? result = fetch_index_all_objects else - result = fetch_index_filtered_objects(filter_keys) + result = fetch_index_filtered_objects(filter_params) end instance_variable_set("@#{plural_name}", result)