Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

Commit

Permalink
Added view model for the Project in detail
Browse files Browse the repository at this point in the history
  • Loading branch information
nakhbari committed Apr 17, 2018
1 parent 2282511 commit 5bb5535
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
8 changes: 8 additions & 0 deletions app/features-json/project_json_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative "../shared/authenticated_controller_base"
require_relative "./view_models/project_summary_view_model"
require_relative "./view_models/project_view_model"

module FastlaneCI
# Controller for providing all data relating to projects
Expand All @@ -17,5 +18,12 @@ class ProjectJSONController < AuthenticatedControllerBase

return all_projects_view_models.to_json
end

get "#{HOME}/:project_id" do |project_id|
project = user_project_with_id(project_id: params[:project_id])
project_view_model = ProjectViewModel.new(project: project)

return project_view_model.to_json
end
end
end
34 changes: 34 additions & 0 deletions app/features-json/view_models/build_summary_view_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require_relative "../../shared/models/build"
require_relative "../../shared/json_convertible"

module FastlaneCI
# View model to expose the basic info about a build.
class BuildSummaryViewModel
include FastlaneCI::JSONConvertible

# @return [Integer]
attr_accessor :number

# @return [String]
attr_reader :status

# @return [Integer]
attr_reader :duration

# @return [String] The git sha of the commit this build was run for
attr_reader :sha

# @return [DateTime] Start time
attr_reader :timestamp

def initialize(build:)
raise "Incorrect object type. Expected Build, got #{build.class}" unless build.kind_of?(Build)

@number = build.number
@status = build.status
@duration = build.duration
@sha = build.sha
@timestamp = build.timestamp
end
end
end
27 changes: 27 additions & 0 deletions app/features-json/view_models/project_view_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require_relative "./build_summary_view_model"
require_relative "../../shared/models/project"
require_relative "../../shared/json_convertible"

module FastlaneCI
# View model to expose the basic info about a project.
class ProjectViewModel
include FastlaneCI::JSONConvertible

# @return [String] Name of the project, also shows up in status as "fastlane.ci: #{project_name}"
attr_accessor :name

# @return [String] Is a UUID so we're not open to ID guessing attacks
attr_reader :id

# @return [Array[BuildSummaryViewModel]]
attr_reader :builds

def initialize(project:)
raise "Incorrect object type. Expected Project, got #{project.class}" unless project.kind_of?(Project)

@name = project.project_name
@id = project.id
@builds = project.builds.map { |build| BuildSummaryViewModel.new(build: build) }
end
end
end
2 changes: 1 addition & 1 deletion app/shared/json_convertible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def self.included(base)

# add these as instance methods
module InstanceMethods
def to_json(options)
def to_json(options = {})
to_object_dictionary.to_json(options)
end

Expand Down

0 comments on commit 5bb5535

Please sign in to comment.