Commit f1e9c97d authored by Grzegorz Bizon's avatar Grzegorz Bizon

Use entity request object in environment entity

parent acfe3940
...@@ -9,9 +9,17 @@ class EnvironmentEntity < Grape::Entity ...@@ -9,9 +9,17 @@ class EnvironmentEntity < Grape::Entity
as: :deployment, as: :deployment,
using: API::Entities::Deployment using: API::Entities::Deployment
expose :environment_path expose :gitlab_path do |environment|
namespace_project_environment_path(
environment.project.namespace,
environment.project,
environment
)
end
expose :can_read?
def environment_path def can_read?
request.path Ability.allowed?(request.user, :read_environment, @object)
end end
end end
class ProjectEntity < Grape::Entity class ProjectEntity < Grape::Entity
include RequestAwareEntity
expose :id expose :id
expose :name expose :name
expose :test do |project| expose :test do |project|
'something' request.user.email
end end
end end
module RequestAwareEntity module RequestAwareEntity
# We use SerializableRequest class to collect parameters and variables
# from the controller. Because options that are being passed to the entity
# are appear in each entity in the chain, we need a way to access data
# that is present in the controller (see #20045).
#
def request def request
options[:request] || options[:request] ||
raise(StandardError, 'Request not set!!') raise(StandardError, 'Request not set!!')
......
...@@ -2,18 +2,21 @@ require 'spec_helper' ...@@ -2,18 +2,21 @@ require 'spec_helper'
describe EnvironmentSerializer do describe EnvironmentSerializer do
let(:serializer) do let(:serializer) do
described_class.new(path: 'some path').represent(resource) described_class.new(path: 'some path', user: user)
.represent(resource)
end end
let(:user) { create(:user) }
context 'when there is a single object provided' do context 'when there is a single object provided' do
let(:resource) { create(:environment) } let(:resource) { create(:environment) }
it 'shows json' do it 'shows json' do
puts serializer.to_json puts serializer.as_json
end end
it 'it generates payload for single object' do it 'it generates payload for single object' do
expect(parsed_json).to be_an_instance_of Hash expect(serializer.as_json).to be_an_instance_of Hash
end end
end end
...@@ -21,15 +24,11 @@ describe EnvironmentSerializer do ...@@ -21,15 +24,11 @@ describe EnvironmentSerializer do
let(:resource) { create_list(:environment, 2) } let(:resource) { create_list(:environment, 2) }
it 'shows json' do it 'shows json' do
puts serializer.to_json puts serializer.as_json
end end
it 'generates payload for collection' do it 'generates payload for collection' do
expect(parsed_json).to be_an_instance_of Array expect(serializer.as_json).to be_an_instance_of Array
end end
end end
def parsed_json
JSON.parse(serializer.to_json)
end
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