diff options
author | James Lopez <james@jameslopez.es> | 2016-11-07 10:08:18 +0100 |
---|---|---|
committer | James Lopez <james@jameslopez.es> | 2016-11-17 08:22:57 +0100 |
commit | 192918cde949c8399d7d526a638f6e09f9fb7a5a (patch) | |
tree | 678d29be77beb72e782e713b8dcfddf9c93023d1 /app/controllers | |
parent | b4bb33c5b7f30dedbb56f4b9f7dbc9fd58d4bdb9 (diff) | |
download | gitlab-ce-192918cde949c8399d7d526a638f6e09f9fb7a5a.tar.gz |
refactored duplicated code in controller, updated JSON array naming and fixed specs
Diffstat (limited to 'app/controllers')
3 files changed, 22 insertions, 35 deletions
diff --git a/app/controllers/concerns/cycle_analytics_params.rb b/app/controllers/concerns/cycle_analytics_params.rb new file mode 100644 index 00000000000..2aaf8f2b451 --- /dev/null +++ b/app/controllers/concerns/cycle_analytics_params.rb @@ -0,0 +1,7 @@ +module CycleAnalyticsParams + extend ActiveSupport::Concern + + def start_date(params) + params[:start_date] == '30' ? 30.days.ago : 90.days.ago + end +end diff --git a/app/controllers/projects/cycle_analytics/events_controller.rb b/app/controllers/projects/cycle_analytics/events_controller.rb index 0f0fd17c8a7..5ef1f911fc5 100644 --- a/app/controllers/projects/cycle_analytics/events_controller.rb +++ b/app/controllers/projects/cycle_analytics/events_controller.rb @@ -1,57 +1,44 @@ class Projects::CycleAnalytics::EventsController < Projects::ApplicationController - # TODO: fix authorization - # before_action :authorize_read_cycle_analytics! + include CycleAnalyticsParams - # TODO: refactor +event_hash+ + before_action :authorize_read_cycle_analytics! def issue - render_events(issues: events.issue_events) + render_events(events.issue_events) end def plan - render_events(commits: events.plan_events) + render_events(events.plan_events) end def code - render_events(merge_requests: events.code_events) + render_events(events.code_events) end def test - @opts = { from: start_date, branch: events_params[:branch_name] } + @opts = { from: start_date(events_params), branch: events_params[:branch_name] } - render_events(builds: events.test_events) + render_events(events.test_events) end def review - render_events(merge_requests: events.review_events) + render_events(events.review_events) end def staging - render_events(builds: events.staging_events) + render_events(events.staging_events) end def production - render_events(issues: events.production_events) + render_events(events.production_events) end private - def render_events(event_hash) + def render_events(events) respond_to do |format| format.html - format.json { render json: event_hash } - end - end - - # TODO refactor this - def start_date - case events_params[:start_date] - when '30' then - 30.days.ago - when '90' then - 90.days.ago - else - 90.days.ago + format.json { render json: { items: events } } end end @@ -60,7 +47,7 @@ class Projects::CycleAnalytics::EventsController < Projects::ApplicationControll end def options - @opts ||= { from: start_date } + @opts ||= { from: start_date(events_params) } end def events_params diff --git a/app/controllers/projects/cycle_analytics_controller.rb b/app/controllers/projects/cycle_analytics_controller.rb index 04f4cc7000e..96eb75a0547 100644 --- a/app/controllers/projects/cycle_analytics_controller.rb +++ b/app/controllers/projects/cycle_analytics_controller.rb @@ -1,11 +1,12 @@ class Projects::CycleAnalyticsController < Projects::ApplicationController include ActionView::Helpers::DateHelper include ActionView::Helpers::TextHelper + include CycleAnalyticsParams before_action :authorize_read_cycle_analytics! def show - @cycle_analytics = ::CycleAnalytics.new(@project, from: parse_start_date) + @cycle_analytics = ::CycleAnalytics.new(@project, from: start_date(cycle_analytics_params)) respond_to do |format| format.html @@ -15,14 +16,6 @@ class Projects::CycleAnalyticsController < Projects::ApplicationController private - def parse_start_date - case cycle_analytics_params[:start_date] - when '30' then 30.days.ago - when '90' then 90.days.ago - else 90.days.ago - end - end - def cycle_analytics_params return {} unless params[:cycle_analytics].present? |