diff --git a/app/views/faalis/dashboard/resource/show.html.slim b/app/views/faalis/dashboard/resource/show.html.slim index 8a7a9e3d..c953d7a3 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_controller.each do |mdl| + li = link_to(t(mdl), controller: mdl, "#{controller_name.singularize}_id" => @resource.id) + .row .col-sm-12.col-lg-12 .box.box-primary 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 eee9e24c..c79ea286 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/resource_show.rb b/lib/faalis/dashboard/sections/resource_show.rb index a16ae975..f5474022 100644 --- a/lib/faalis/dashboard/sections/resource_show.rb +++ b/lib/faalis/dashboard/sections/resource_show.rb @@ -14,6 +14,19 @@ def show @resource_title = t(_resource_title.singularize) + # 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) return if _override_views.include? :show diff --git a/lib/faalis/dashboard/sections/resources_index.rb b/lib/faalis/dashboard/sections/resources_index.rb index 508a1c89..bf5d3bc5 100644 --- a/lib/faalis/dashboard/sections/resources_index.rb +++ b/lib/faalis/dashboard/sections/resources_index.rb @@ -20,57 +20,76 @@ 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 - puts "<<<<<<<" * 100, scope + # If user provided an scope for `index` section. if !scope.nil? - # If user provided an scope for `index` section. - - 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) + scope = scope.order('created_at DESC').page(params[:page]) + policy_scope(scope) + + end + + def fetch_index_filtered_objects(filter_params) + items = fetch_index_all_objects + filter_params.each do |v| + items = items.where(v => params[v]) end + items + end + + def index_properties + Faalis::Dashboard::DSL::Index.new(model) + end private def fetch_and_set_all - result = fetch_index_objects + # rename ---- fetch_all_index_objects + + # 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_params.empty? + result = fetch_index_all_objects + else + result = fetch_index_filtered_objects(filter_params) + end instance_variable_set("@#{plural_name}", result) @index_fields = index_properties.fields @resources = result end - # You can override this method to change the behaviour of `index` - # action - def index_hook(resources) + # You can override this method to change the behaviour of `index` + # action + def index_hook(resources) - end + end # The actual DSL for index ages module ClassMethods