From c27488ac85759f0681a073086b303e6775736406 Mon Sep 17 00:00:00 2001 From: Xaun Lopez Date: Tue, 31 Oct 2017 15:26:35 +0000 Subject: [PATCH 1/2] Remove tenant_dashboards, add dashboard owner relationship --- .../v1/impac/dashboards/index.json.jbuilder | 2 +- .../v1/impac/dashboards/show.json.jbuilder | 2 +- .../jpi/v1/impac/dashboards_controller.rb | 27 ++++++++----------- core/app/models/mno_enterprise/dashboard.rb | 2 ++ 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/api/app/views/mno_enterprise/jpi/v1/impac/dashboards/index.json.jbuilder b/api/app/views/mno_enterprise/jpi/v1/impac/dashboards/index.json.jbuilder index 7f1c2cca3..7620a1b53 100644 --- a/api/app/views/mno_enterprise/jpi/v1/impac/dashboards/index.json.jbuilder +++ b/api/app/views/mno_enterprise/jpi/v1/impac/dashboards/index.json.jbuilder @@ -1 +1 @@ -json.array! @dashboards, partial: 'dashboard', as: :dashboard \ No newline at end of file +json.array! @dashboards, partial: 'dashboard', as: :dashboard diff --git a/api/app/views/mno_enterprise/jpi/v1/impac/dashboards/show.json.jbuilder b/api/app/views/mno_enterprise/jpi/v1/impac/dashboards/show.json.jbuilder index 180e1581a..bb62d73d5 100644 --- a/api/app/views/mno_enterprise/jpi/v1/impac/dashboards/show.json.jbuilder +++ b/api/app/views/mno_enterprise/jpi/v1/impac/dashboards/show.json.jbuilder @@ -1 +1 @@ -json.partial! 'dashboard', dashboard: @dashboard \ No newline at end of file +json.partial! 'dashboard', dashboard: @dashboard diff --git a/api/lib/mno_enterprise/concerns/controllers/jpi/v1/impac/dashboards_controller.rb b/api/lib/mno_enterprise/concerns/controllers/jpi/v1/impac/dashboards_controller.rb index d7f6d361d..9a164ad47 100644 --- a/api/lib/mno_enterprise/concerns/controllers/jpi/v1/impac/dashboards_controller.rb +++ b/api/lib/mno_enterprise/concerns/controllers/jpi/v1/impac/dashboards_controller.rb @@ -29,14 +29,12 @@ def show # POST /mnoe/jpi/v1/impac/dashboards # -> POST /api/mnoe/v1/users/1/dashboards def create - # TODO: dashboards.build breaks as dashboard.organization_ids returns nil, instead of an - # empty array. (see MnoEnterprise::Impac::Dashboard #organizations) - # @dashboard = dashboards.build(dashboard_create_params) # TODO: enable authorization # authorize! :manage_dashboard, @dashboard - # if @dashboard.save - @dashboard = MnoEnterprise::Dashboard.create!(dashboard_create_params) - MnoEnterprise::EventLogger.info('dashboard_create', current_user.id, 'Dashboard Creation', @dashboard) + dashboard = MnoEnterprise::Dashboard.new(dashboard_params) + dashboard.relationships.owner = current_user + dashboard.save! + MnoEnterprise::EventLogger.info('dashboard_create', current_user.id, 'Dashboard Creation', dashboard) @dashboard = dashboard.load_required(*DASHBOARD_DEPENDENCIES) render 'show' end @@ -48,7 +46,7 @@ def update # TODO: enable authorization # authorize! :manage_dashboard, dashboard - dashboard.update_attributes!(dashboard_update_params) + dashboard.update_attributes!(dashboard_params) # Reload Dashboard @dashboard = dashboard.load_required(DASHBOARD_DEPENDENCIES) @@ -82,13 +80,14 @@ def copy private def dashboard(*included) - # TODO: [APIv2] Improve filtering by owner (owner_type?) - @dashboard ||= MnoEnterprise::Dashboard.where(owner_id: current_user.id).includes(included).find(params[:id].to_i).first + @dashboard ||= MnoEnterprise::Dashboard.where(id: params[:id]) + .includes(included) + .first end def dashboards - # TODO: [APIv2] Improve filtering by owner (owner_type?) - @dashboards ||= MnoEnterprise::Dashboard.includes(*DASHBOARD_DEPENDENCIES).find(owner_id: current_user.id) + @dashboards ||= MnoEnterprise::Dashboard.where(owner_id: current_user.id) + .includes(*DASHBOARD_DEPENDENCIES) end def templates @@ -96,7 +95,7 @@ def templates end def template - @template ||= MnoEnterprise::Dashboard.templates.find(params[:id].to_i).first + @template ||= MnoEnterprise::Dashboard.templates.find(params[:id]).first end def whitelisted_params @@ -110,9 +109,5 @@ def dashboard_params whitelisted[:settings] = params[:dashboard][:metadata] || {} end .except(:metadata) - .merge(owner_type: "User", owner_id: current_user.id) end - alias :dashboard_update_params :dashboard_params - alias :dashboard_create_params :dashboard_params - end diff --git a/core/app/models/mno_enterprise/dashboard.rb b/core/app/models/mno_enterprise/dashboard.rb index 9bd42a4c7..8509c477f 100644 --- a/core/app/models/mno_enterprise/dashboard.rb +++ b/core/app/models/mno_enterprise/dashboard.rb @@ -5,6 +5,8 @@ class Dashboard < BaseResource property :updated_at, type: :time property :owner_id, type: :string + has_one :owner + # TODO: APIv2 - Is this needed? # default_scope -> { where(dashboard_type: 'dashboard') } From 42673ad39b3d467715578c6e980461de8155d0fa Mon Sep 17 00:00:00 2001 From: Xaun Lopez Date: Tue, 31 Oct 2017 16:37:31 +0000 Subject: [PATCH 2/2] Fully qualify dashboard template paths Allows concern to be included in different modules --- .../jpi/v1/impac/dashboards/index.json.jbuilder | 3 ++- .../jpi/v1/impac/dashboards/show.json.jbuilder | 3 ++- .../jpi/v1/impac/dashboards_controller.rb | 14 ++++++++++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/api/app/views/mno_enterprise/jpi/v1/impac/dashboards/index.json.jbuilder b/api/app/views/mno_enterprise/jpi/v1/impac/dashboards/index.json.jbuilder index 7620a1b53..9f1948b37 100644 --- a/api/app/views/mno_enterprise/jpi/v1/impac/dashboards/index.json.jbuilder +++ b/api/app/views/mno_enterprise/jpi/v1/impac/dashboards/index.json.jbuilder @@ -1 +1,2 @@ -json.array! @dashboards, partial: 'dashboard', as: :dashboard +# Fully qualify template path to allow concern to be included in different modules +json.array! @dashboards, partial: 'mno_enterprise/jpi/v1/impac/dashboards/dashboard', as: :dashboard diff --git a/api/app/views/mno_enterprise/jpi/v1/impac/dashboards/show.json.jbuilder b/api/app/views/mno_enterprise/jpi/v1/impac/dashboards/show.json.jbuilder index bb62d73d5..90c23430f 100644 --- a/api/app/views/mno_enterprise/jpi/v1/impac/dashboards/show.json.jbuilder +++ b/api/app/views/mno_enterprise/jpi/v1/impac/dashboards/show.json.jbuilder @@ -1 +1,2 @@ -json.partial! 'dashboard', dashboard: @dashboard +# Fully qualify template path to allow concern to be included in different modules +json.partial! 'mno_enterprise/jpi/v1/impac/dashboards/dashboard', dashboard: @dashboard diff --git a/api/lib/mno_enterprise/concerns/controllers/jpi/v1/impac/dashboards_controller.rb b/api/lib/mno_enterprise/concerns/controllers/jpi/v1/impac/dashboards_controller.rb index 9a164ad47..244ebed33 100644 --- a/api/lib/mno_enterprise/concerns/controllers/jpi/v1/impac/dashboards_controller.rb +++ b/api/lib/mno_enterprise/concerns/controllers/jpi/v1/impac/dashboards_controller.rb @@ -18,12 +18,18 @@ module MnoEnterprise::Concerns::Controllers::Jpi::V1::Impac::DashboardsControlle # GET /mnoe/jpi/v1/impac/dashboards def index dashboards + + # Fully qualify template path to allow concern to be included in different modules + render template: 'mno_enterprise/jpi/v1/impac/dashboards/index' end # GET /mnoe/jpi/v1/impac/dashboards/1 # -> GET /api/mnoe/v1/users/1/dashboards def show render_not_found('dashboard') unless dashboard(*DASHBOARD_DEPENDENCIES) + + # Fully qualify template path to allow concern to be included in different modules + render template: 'mno_enterprise/jpi/v1/impac/dashboards/show' end # POST /mnoe/jpi/v1/impac/dashboards @@ -36,7 +42,9 @@ def create dashboard.save! MnoEnterprise::EventLogger.info('dashboard_create', current_user.id, 'Dashboard Creation', dashboard) @dashboard = dashboard.load_required(*DASHBOARD_DEPENDENCIES) - render 'show' + + # Fully qualify template path to allow concern to be included in different modules + render template: 'mno_enterprise/jpi/v1/impac/dashboards/show' end # PUT /mnoe/jpi/v1/impac/dashboards/1 @@ -50,7 +58,9 @@ def update # Reload Dashboard @dashboard = dashboard.load_required(DASHBOARD_DEPENDENCIES) - render 'show' + + # Fully qualify template path to allow concern to be included in different modules + render template: 'mno_enterprise/jpi/v1/impac/dashboards/show' end # DELETE /mnoe/jpi/v1/impac/dashboards/1