Skip to content
Snippets Groups Projects
Commit 67673262 authored by Peter Leitzen's avatar Peter Leitzen Committed by Thong Kuah
Browse files

Use ActiveModel's type instead of virtus

The virtus project has been discontinued:

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