refactored duplicated code in controller, updated JSON array naming and fixed specs

This commit is contained in:
James Lopez 2016-11-07 10:08:18 +01:00
parent b4bb33c5b7
commit 192918cde9
4 changed files with 36 additions and 49 deletions

View file

@ -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

View file

@ -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

View file

@ -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?

View file

@ -17,17 +17,17 @@ describe 'cycle analytics events' do
it 'lists the issue events' do
get namespace_project_cycle_analytics_issue_path(project.namespace, project, format: :json)
expect(json_response['issues']).not_to be_empty
expect(json_response['items']).not_to be_empty
first_issue_iid = Issue.order(created_at: :desc).pluck(:iid).first.to_s
expect(json_response['issues'].first['iid']).to eq(first_issue_iid)
expect(json_response['items'].first['iid']).to eq(first_issue_iid)
end
it 'lists the plan events' do
get namespace_project_cycle_analytics_plan_path(project.namespace, project, format: :json)
expect(json_response['commits']).not_to be_empty
expect(json_response['items']).not_to be_empty
commits = []
@ -39,53 +39,53 @@ describe 'cycle analytics events' do
newest_sha = commits.sort_by { |k| k['date'] }.first[:sha][0...8]
expect(json_response['commits'].first['sha']).to eq(newest_sha)
expect(json_response['items'].first['sha']).to eq(newest_sha)
end
it 'lists the code events' do
get namespace_project_cycle_analytics_code_path(project.namespace, project, format: :json)
expect(json_response['merge_requests']).not_to be_empty
expect(json_response['items']).not_to be_empty
first_mr_iid = Issue.order(created_at: :desc).pluck(:iid).first.to_s
expect(json_response['merge_requests'].first['iid']).to eq(first_mr_iid)
expect(json_response['items'].first['iid']).to eq(first_mr_iid)
end
it 'lists the test events' do
get namespace_project_cycle_analytics_test_path(project.namespace, project, format: :json)
expect(json_response['builds']).not_to be_empty
expect(json_response['items']).not_to be_empty
expect(json_response['builds'].first['date']).not_to be_empty
expect(json_response['items'].first['date']).not_to be_empty
end
it 'lists the review events' do
get namespace_project_cycle_analytics_review_path(project.namespace, project, format: :json)
expect(json_response['merge_requests']).not_to be_empty
expect(json_response['items']).not_to be_empty
first_mr_iid = Issue.order(created_at: :desc).pluck(:iid).first.to_s
expect(json_response['merge_requests'].first['iid']).to eq(first_mr_iid)
expect(json_response['items'].first['iid']).to eq(first_mr_iid)
end
it 'lists the staging events' do
get namespace_project_cycle_analytics_staging_path(project.namespace, project, format: :json)
expect(json_response['builds']).not_to be_empty
expect(json_response['items']).not_to be_empty
expect(json_response['builds'].first['date']).not_to be_empty
expect(json_response['items'].first['date']).not_to be_empty
end
it 'lists the production events' do
get namespace_project_cycle_analytics_production_path(project.namespace, project, format: :json)
expect(json_response['issues']).not_to be_empty
expect(json_response['items']).not_to be_empty
first_issue_iid = Issue.order(created_at: :desc).pluck(:iid).first.to_s
expect(json_response['issues'].first['iid']).to eq(first_issue_iid)
expect(json_response['items'].first['iid']).to eq(first_issue_iid)
end
end