Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd29258
commit d9c0918
Showing
18 changed files
with
291 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
lib/graphql_devise/mount_method/operation_preparers/custom_operation_preparer.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module GraphqlDevise | ||
module MountMethod | ||
module OperationPreparers | ||
class CustomOperationPreparer | ||
def initialize(selected_keys:, custom_operations:, mapping_name:) | ||
@selected_keys = selected_keys | ||
@custom_operations = custom_operations | ||
@mapping_name = mapping_name | ||
end | ||
|
||
def call | ||
@custom_operations.slice(*@selected_keys).each_with_object({}) do |(action, operation), result| | ||
mapped_action = "#{@mapping_name}_#{action}" | ||
|
||
result[mapped_action.to_sym] = [ | ||
OperationPreparers::GqlNameSetter.new(mapped_action), | ||
OperationPreparers::ResourceNameSetter.new(@mapping_name) | ||
].reduce(operation) { |prepared_operation, preparer| preparer.call(prepared_operation) } | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
19 changes: 0 additions & 19 deletions
19
lib/graphql_devise/mount_method/operation_preparers/default_operation_creator.rb
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
lib/graphql_devise/mount_method/operation_preparers/default_operation_preparer.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module GraphqlDevise | ||
module MountMethod | ||
module OperationPreparers | ||
class DefaultOperationPreparer | ||
def initialize(selected_operations:, custom_keys:, mapping_name:, preparer:) | ||
@selected_operations = selected_operations | ||
@custom_keys = custom_keys | ||
@mapping_name = mapping_name | ||
@preparer = preparer | ||
end | ||
|
||
def call | ||
@selected_operations.except(*@custom_keys).each_with_object({}) do |(action, operation), result| | ||
mapped_action = "#{@mapping_name}_#{action}" | ||
|
||
result[mapped_action.to_sym] = [ | ||
OperationPreparers::GqlNameSetter.new(mapped_action), | ||
@preparer, | ||
OperationPreparers::ResourceNameSetter.new(@mapping_name) | ||
].reduce(child_class(operation)) { |prepared_operation, preparer| preparer.call(prepared_operation) } | ||
end | ||
end | ||
|
||
private | ||
|
||
def child_class(operation) | ||
Class.new(operation) | ||
end | ||
end | ||
end | ||
end | ||
end |
23 changes: 23 additions & 0 deletions
23
lib/graphql_devise/mount_method/operation_preparers/gql_name_setter.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module GraphqlDevise | ||
module MountMethod | ||
module OperationPreparers | ||
class GqlNameSetter | ||
def initialize(mapping_name) | ||
@mapping_name = mapping_name | ||
end | ||
|
||
def call(operation) | ||
operation.graphql_name(graphql_name) | ||
|
||
operation | ||
end | ||
|
||
private | ||
|
||
def graphql_name | ||
@mapping_name.camelize(:upper) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
spec/services/mount_method/operation_preparers/custom_operation_preparer_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe GraphqlDevise::MountMethod::OperationPreparers::CustomOperationPreparer do | ||
describe '#call' do | ||
subject(:prepared) { described_class.new(selected_keys: selected_keys, custom_operations: operations, mapping_name: mapping_name).call } | ||
|
||
let(:login_operation) { double(:confirm_operation, graphql_name: nil) } | ||
let(:logout_operation) { double(:sign_up_operation, graphql_name: nil) } | ||
let(:mapping_name) { 'user' } | ||
let(:operations) { { login: login_operation, logout: logout_operation, invalid: double(:invalid) } } | ||
let(:selected_keys) { [:login, :logout, :sign_up, :confirm] } | ||
|
||
it 'returns only those operations with no custom operation provided' do | ||
expect(prepared.keys).to contain_exactly(:user_login, :user_logout) | ||
end | ||
|
||
it 'prepares custom operations' do | ||
expect(login_operation).to receive(:graphql_name).with('UserLogin') | ||
expect(logout_operation).to receive(:graphql_name).with('UserLogout') | ||
|
||
prepared | ||
|
||
expect(login_operation.instance_variable_get(:@resource_name)).to eq(:user) | ||
expect(logout_operation.instance_variable_get(:@resource_name)).to eq(:user) | ||
end | ||
|
||
context 'when no selected keys are provided' do | ||
let(:selected_keys) { [] } | ||
|
||
it 'returns no operations' do | ||
expect(prepared).to eq({}) | ||
end | ||
end | ||
|
||
context 'when no custom operations are provided' do | ||
let(:operations) { {} } | ||
|
||
it 'returns no operations' do | ||
expect(prepared).to eq({}) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.