Commit c44614dc authored by Thong Kuah's avatar Thong Kuah

Merge branch 'pl-remove-virtus' into 'master'

Prefer ActiveModel's attributes over Virtus

See merge request gitlab-org/gitlab-ce!31835
parents 51c19691 67673262
...@@ -275,7 +275,6 @@ gem 'font-awesome-rails', '~> 4.7' ...@@ -275,7 +275,6 @@ gem 'font-awesome-rails', '~> 4.7'
gem 'gemojione', '~> 3.3' gem 'gemojione', '~> 3.3'
gem 'gon', '~> 6.2' gem 'gon', '~> 6.2'
gem 'request_store', '~> 1.3' gem 'request_store', '~> 1.3'
gem 'virtus', '~> 1.0.1'
gem 'base32', '~> 0.3.0' gem 'base32', '~> 0.3.0'
# Sentry integration # Sentry integration
......
...@@ -1252,7 +1252,6 @@ DEPENDENCIES ...@@ -1252,7 +1252,6 @@ DEPENDENCIES
unicorn-worker-killer (~> 0.4.4) unicorn-worker-killer (~> 0.4.4)
validates_hostname (~> 1.0.6) validates_hostname (~> 1.0.6)
version_sorter (~> 2.2.4) version_sorter (~> 2.2.4)
virtus (~> 1.0.1)
vmstat (~> 2.3.0) vmstat (~> 2.3.0)
webmock (~> 3.5.1) webmock (~> 3.5.1)
webpack-rails (~> 0.9.10) webpack-rails (~> 0.9.10)
......
...@@ -92,7 +92,7 @@ class Projects::BlobController < Projects::ApplicationController ...@@ -92,7 +92,7 @@ class Projects::BlobController < Projects::ApplicationController
def diff def diff
apply_diff_view_cookie! apply_diff_view_cookie!
@form = Blobs::UnfoldPresenter.new(blob, params.to_unsafe_h) @form = Blobs::UnfoldPresenter.new(blob, diff_params)
# keep only json rendering when # keep only json rendering when
# https://gitlab.com/gitlab-org/gitlab-ce/issues/44988 is done # https://gitlab.com/gitlab-org/gitlab-ce/issues/44988 is done
...@@ -239,4 +239,8 @@ class Projects::BlobController < Projects::ApplicationController ...@@ -239,4 +239,8 @@ class Projects::BlobController < Projects::ApplicationController
def tree_path def tree_path
@path.rpartition('/').first @path.rpartition('/').first
end end
def diff_params
params.permit(:full, :since, :to, :bottom, :unfold, :offset, :indent)
end
end end
# frozen_string_literal: true # frozen_string_literal: true
require 'gt_one_coercion'
module Blobs module Blobs
class UnfoldPresenter < BlobPresenter class UnfoldPresenter < BlobPresenter
include Virtus.model include ActiveModel::Attributes
include ActiveModel::AttributeAssignment
include Gitlab::Utils::StrongMemoize include Gitlab::Utils::StrongMemoize
attribute :full, Boolean, default: false attribute :full, :boolean, default: false
attribute :since, GtOneCoercion attribute :since, :integer, default: 1
attribute :to, Integer attribute :to, :integer, default: 1
attribute :bottom, Boolean attribute :bottom, :boolean, default: false
attribute :unfold, Boolean, default: true attribute :unfold, :boolean, default: true
attribute :offset, Integer attribute :offset, :integer, default: 0
attribute :indent, Integer, default: 0 attribute :indent, :integer, default: 0
alias_method :full?, :full
alias_method :bottom?, :bottom
alias_method :unfold?, :unfold
def initialize(blob, params) def initialize(blob, params)
super(blob)
self.attributes = params
# Load all blob data first as we need to ensure they're all loaded first # Load all blob data first as we need to ensure they're all loaded first
# so we can accurately show the rest of the diff when unfolding. # so we can accurately show the rest of the diff when unfolding.
load_all_blob_data load_all_blob_data
@subject = blob
@all_lines = blob.data.lines @all_lines = blob.data.lines
super(params)
self.attributes = prepare_attributes handle_full_or_end!
end end
# Returns an array of Gitlab::Diff::Line with match line added # Returns an array of Gitlab::Diff::Line with match line added
...@@ -56,21 +60,18 @@ module Blobs ...@@ -56,21 +60,18 @@ module Blobs
private private
def prepare_attributes def handle_full_or_end!
return attributes unless full? || to == -1 return unless full? || to == -1
full_opts = { self.since = 1 if full?
since: 1,
self.attributes = {
to: all_lines_size, to: all_lines_size,
bottom: false, bottom: false,
unfold: false, unfold: false,
offset: 0, offset: 0,
indent: 0 indent: 0
} }
return full_opts if full?
full_opts.merge(attributes.slice(:since))
end end
def all_lines_size def all_lines_size
......
# frozen_string_literal: true
class GtOneCoercion < Virtus::Attribute
def coerce(value)
[1, value.to_i].max
end
end
...@@ -10,16 +10,31 @@ describe Blobs::UnfoldPresenter do ...@@ -10,16 +10,31 @@ describe Blobs::UnfoldPresenter do
let(:subject) { described_class.new(blob, params) } let(:subject) { described_class.new(blob, params) }
describe '#initialize' do describe '#initialize' do
let(:result) { subject }
context 'with empty params' do
let(:params) { {} }
it 'sets default attributes' do
expect(result.full?).to eq(false)
expect(result.since).to eq(1)
expect(result.to).to eq(1)
expect(result.bottom).to eq(false)
expect(result.unfold).to eq(true)
expect(result.offset).to eq(0)
expect(result.indent).to eq(0)
end
end
context 'when full is false' do context 'when full is false' do
let(:params) { { full: false, since: 2, to: 3, bottom: false, offset: 1, indent: 1 } } let(:params) { { full: false, since: 2, to: 3, bottom: false, offset: 1, indent: 1 } }
it 'sets attributes' do it 'sets attributes' do
result = subject
expect(result.full?).to eq(false) expect(result.full?).to eq(false)
expect(result.since).to eq(2) expect(result.since).to eq(2)
expect(result.to).to eq(3) expect(result.to).to eq(3)
expect(result.bottom).to eq(false) expect(result.bottom).to eq(false)
expect(result.unfold).to eq(true)
expect(result.offset).to eq(1) expect(result.offset).to eq(1)
expect(result.indent).to eq(1) expect(result.indent).to eq(1)
end end
...@@ -29,12 +44,11 @@ describe Blobs::UnfoldPresenter do ...@@ -29,12 +44,11 @@ describe Blobs::UnfoldPresenter do
let(:params) { { full: true, since: 2, to: 3, bottom: false, offset: 1, indent: 1 } } let(:params) { { full: true, since: 2, to: 3, bottom: false, offset: 1, indent: 1 } }
it 'sets other attributes' do it 'sets other attributes' do
result = subject
expect(result.full?).to eq(true) expect(result.full?).to eq(true)
expect(result.since).to eq(1) expect(result.since).to eq(1)
expect(result.to).to eq(blob.lines.size) expect(result.to).to eq(blob.lines.size)
expect(result.bottom).to eq(false) expect(result.bottom).to eq(false)
expect(result.unfold).to eq(false)
expect(result.offset).to eq(0) expect(result.offset).to eq(0)
expect(result.indent).to eq(0) expect(result.indent).to eq(0)
end end
...@@ -44,12 +58,11 @@ describe Blobs::UnfoldPresenter do ...@@ -44,12 +58,11 @@ describe Blobs::UnfoldPresenter do
let(:params) { { full: false, since: 2, to: -1, bottom: true, offset: 1, indent: 1 } } let(:params) { { full: false, since: 2, to: -1, bottom: true, offset: 1, indent: 1 } }
it 'sets other attributes' do it 'sets other attributes' do
result = subject
expect(result.full?).to eq(false) expect(result.full?).to eq(false)
expect(result.since).to eq(2) expect(result.since).to eq(2)
expect(result.to).to eq(blob.lines.size) expect(result.to).to eq(blob.lines.size)
expect(result.bottom).to eq(false) expect(result.bottom).to eq(false)
expect(result.unfold).to eq(false)
expect(result.offset).to eq(0) expect(result.offset).to eq(0)
expect(result.indent).to eq(0) expect(result.indent).to eq(0)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment