Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AI-1] Context attributes assigned in interactor not accessible as element within interactor #200

Closed
dark-panda opened this issue Feb 19, 2020 · 4 comments · Fixed by #241
Labels
bug Something isn't working verified This issue has been verified.
Milestone

Comments

@dark-panda
Copy link

class InteractorA < ActiveInteractor::Base
  def perform
    context.foo = 'hello'
    context.bar = 'world'
    context.baz = '!!!!!'

    puts "InteractorA"
    puts "foo => #{context.foo}"
    puts "bar => #{context.bar}"
    puts "baz => #{context.baz}"
    puts "foo => #{context[:foo]}"
    puts "bar => #{context[:bar]}"
    puts "baz => #{context[:baz]}"
  end
end

class InteractorA::Context < ActiveInteractor::Context::Base
  attribute :foo
end

InteractorA.perform

# outputs
# foo => hello
# bar => world
# baz => !!!!!
# foo =>
# bar => world
# baz => !!!!!
@dark-panda dark-panda added bug Something isn't working unverified This issue has not been verified labels Feb 19, 2020
@aaronmallen aaronmallen added verified This issue has been verified. jira and removed unverified This issue has not been verified labels Feb 20, 2020
@aaronmallen
Copy link
Owner

aaronmallen commented Feb 20, 2020

[AI-1]

@aaronmallen aaronmallen changed the title Context attributes assigned in interactor not accessible as element within interactor [AI-32] Context attributes assigned in interactor not accessible as element within interactor Feb 20, 2020
@aaronmallen aaronmallen changed the title [AI-32] Context attributes assigned in interactor not accessible as element within interactor [AI-1] Context attributes assigned in interactor not accessible as element within interactor Feb 27, 2020
@aka-momo
Copy link
Contributor

aka-momo commented Mar 7, 2020

Same issue occurs in the the organizer context. All the attributes defined in contexts are not exposed.

class TestInteractor < ActiveInteractor::Base
  def perform
    context.foo = "hello"
    context.bar = 'world'
  end
end

class TestInteractor::Context < ActiveInteractor::Context::Base
  attribute :foo
  attribute :baz, default: -> { '!' }
end

class TestOrganizer < ActiveInteractor::Organizer::Base
  organize :test_interactor
end

p TestOrganizer.perform
#<TestOrganizer::Context bar="world">

@aaronmallen aaronmallen added this to the v1.0.5 milestone Mar 9, 2020
@majksner
Copy link

@dark-panda @mohameddiaa27 As a temporary workaround you can do something like this:

class TestInteractor < ActiveInteractor::Base
  before_perform :user_exists?

  def perform
    context.user = User.create_with(
      first_name: context.first_name,
      last_name: context.last_name
    ).find_or_create_by(email: context.email)
  end

  private

  def user_exists?
   return unless User.where(email: context.email).exists?
   
   context.errors.add(:user, " already exists")
   context.fail!
  end
end

@aaronmallen
Copy link
Owner

@dark-panda would you mind providing a code review on #236

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working verified This issue has been verified.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants