Skip to content
This repository has been archived by the owner on Nov 19, 2018. It is now read-only.

Decorators updating & simplifying #52

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions app/decorators/branch_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ class BranchDecorator < Draper::Decorator
delegate_all

def as_json(*args)
{
id: id,
name: name,
order_id: order_id,
path: path,
created_at: created_at.iso8601,
updated_at: updated_at.iso8601,
count: srpms.count
}
super only: [:id, :name, :order_id, :path], methods: [:created_at, :updated_at, :count]
end

private

def count
srpms.count
end

def updated_at
model.updated_at.iso8601
end

def created_at
model.created_at.iso8601
end
end
25 changes: 12 additions & 13 deletions app/decorators/bug_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,8 @@ class BugDecorator < Draper::Decorator
delegate_all

def as_json(*args)
{
bug_id: bug_id,
bug_status: bug_status,
resolution: resolution,
bug_severity: bug_severity,
product: product,
component: component,
assigned_to: assigned_to,
reporter: reporter,
short_desc: short_desc,
created_at: created_at.iso8601,
updated_at: updated_at.iso8601
}
super only: [:bug_id, :bug_status, :resolution, :bug_severity, :product, :component, :assigned_to, :reporter, :short_desc],
methods: [:updated_at, :created_at]
end

def bugzilla_url
Expand All @@ -24,4 +13,14 @@ def bugzilla_url
def link_to_bugzilla
h.link_to(bug_id, bugzilla_url, class: 'news')
end

private

def created_at
model.created_at.iso8601
end

def updated_at
model.updated_at.iso8601
end
end
20 changes: 11 additions & 9 deletions app/decorators/changelog_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ class ChangelogDecorator < Draper::Decorator
delegate_all

def as_json(*args)
{
id: id,
srpm_id: srpm_id,
changelogtime: changelogtime,
changelogname: changelogname,
changelogtext: changelogtext,
created_at: created_at.iso8601,
updated_at: updated_at.iso8601
}
super only: [:id, :srpm_id, :changelogtime, :changelogname, :changelogtext], methods: [:created_at, :updated_at]
end

private

def created_at
model.created_at.iso8601
end

def updated_at
model.updated_at.iso8601
end
end
22 changes: 9 additions & 13 deletions app/decorators/maintainer_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,7 @@ class MaintainerDecorator < Draper::Decorator
delegate_all

def as_json(*args)
{
id: id,
name: name,
email: email,
login: login,
time_zone: time_zone,
jabber: jabber,
info: info,
website: website,
location: location,
created_at: created_at.iso8601,
updated_at: updated_at.iso8601
}
super only: [:id, :name, :email, :login, :time_zone, :jabber, :info, :website, :location], methods: [:created_at, :updated_at]
end

def avatar_url
Expand All @@ -26,4 +14,12 @@ def avatar_url
def gravatar_id
Digest::MD5.hexdigest(email.downcase)
end

def created_at
model.created_at.iso8601
end

def updated_at
model.updated_at.iso8601
end
end
39 changes: 17 additions & 22 deletions app/decorators/package_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,22 @@ class PackageDecorator < Draper::Decorator
delegate_all

def as_json(*args)
{
id: id,
srpm_id: srpm_id,
name: name,
version: version,
release: release,
epoch: epoch,
arch: arch,
summary: summary,
license: license,
url: url,
description: description,
buildtime: buildtime.iso8601,
group_id: group_id,
md5: md5,
groupname: groupname,
size: size,
filename: filename,
sourcepackage: sourcepackage,
created_at: created_at.iso8601,
updated_at: updated_at.iso8601
}
super only: [:id, :srpm_id, :name, :version, :release, :epoch, :arch, :summary, :license, :url, :description, :group_id, :md5,
:groupname, :sourcepackage, :size, :filename],
methods: [:buildtime, :created_at, :updated_at]
end

private

def buildtime
model.buildtime.iso8601
end

def updated_at
model.updated_at.iso8601
end

def created_at
model.created_at.iso8601
end
end
3 changes: 3 additions & 0 deletions app/models/branch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ class Branch < ApplicationRecord

validates :vendor, presence: true

#
# TODO: enable counter cache for me!!!
#
has_many :srpms

has_many :changelogs, through: :srpms
Expand Down
2 changes: 1 addition & 1 deletion app/models/bug.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Bug < ApplicationRecord
validates :bug_id, presence: true
validates :bug_id, presence: true, numericality: { only_integer: true }
end
2 changes: 1 addition & 1 deletion spec/decorators/branch_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
end
end

subject { branch.decorate.as_json }
subject { branch.decorate.as_json.symbolize_keys }

its([:id]) { should eq(123) }

Expand Down
2 changes: 1 addition & 1 deletion spec/decorators/bug_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

before { expect(updated_at).to receive(:iso8601).and_return(updated_at) }

subject { bug.decorate.as_json }
subject { bug.decorate.as_json.symbolize_keys }

its([:bug_id]) { should eq(22_555) }

Expand Down
2 changes: 1 addition & 1 deletion spec/decorators/changelog_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
updated_at: updated_at
end

subject { changelog.decorate.as_json }
subject { changelog.decorate.as_json.symbolize_keys }

its([:id]) { should eq(321) }

Expand Down
2 changes: 1 addition & 1 deletion spec/decorators/maintainer_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
updated_at: updated_at
end

subject { maintainer.decorate.as_json }
subject { maintainer.decorate.as_json.symbolize_keys }

its([:id]) { should eq(3) }

Expand Down
71 changes: 71 additions & 0 deletions spec/decorators/package_decorator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require 'rails_helper'

describe PackageDecorator do
describe '#as_json' do
let(:created_at) { double }

let(:updated_at) { double }

let(:buildtime) { double }

let(:package) do
stub_model Package, id: 123, filename: 'azaza', srpm_id: 2, name: 'sudo', version: '1', release: 'fail', epoch: 2, arch: '3',
summary: 'awesome package', license: 'MIT', url: 'www', description: 'thats all', group_id: 3, md5: 'blablabla',
groupname: 'main', sourcepackage: 'nope', size: 1
end

before { expect(package).to receive(:created_at).and_return(created_at) }

before { expect(package).to receive(:buildtime).and_return(buildtime) }

before { expect(package).to receive(:updated_at).and_return(updated_at) }

before { expect(created_at).to receive(:iso8601).and_return(created_at) }

before { expect(updated_at).to receive(:iso8601).and_return(updated_at) }

before { expect(buildtime).to receive(:iso8601).and_return(buildtime) }

subject { package.decorate.as_json.symbolize_keys }

its([:id]) { should eq(123) }

its([:filename]) { should eq('azaza') }

its([:srpm_id]) { should eq(2) }

its([:name]) { should eq('sudo') }

its([:version]) { should eq('1') }

its([:release]) { should eq('fail') }

its([:epoch]) { should eq(2) }

its([:arch]) { should eq('3') }

its([:summary]) { should eq('awesome package') }

its([:license]) { should eq('MIT') }

its([:url]) { should eq('www') }

its([:description]) { should eq('thats all') }

its([:group_id]) { should eq(3) }

its([:md5]) { should eq('blablabla') }

its([:groupname]) { should eq('main') }

its([:sourcepackage]) { should eq('nope') }

its([:size]) { should eq(1) }

its([:buildtime]) { should eq(buildtime) }

its([:created_at]) { should eq(created_at) }

its([:updated_at]) { should eq(updated_at) }
end
end
2 changes: 2 additions & 0 deletions spec/models/bug_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

describe 'Validations' do
it { should validate_presence_of(:bug_id) }

it { should validate_numericality_of(:bug_id).only_integer }
end

describe 'DB Indexes' do
Expand Down