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

Use superclass for CSV method #313

Merged
merged 2 commits into from
Jan 25, 2021
Merged

Conversation

adrianna-chang-shopify
Copy link
Contributor

Change the implementation of CsvTask to be a class from which application Tasks can inherit from, instead of a mixin.

It was unclear to me whether we're okay keeping the use of respond_to?(csv_content=) in TaskJob when setting csv_content on a Task:

@task = Task.named(@run.task_name).new
if @task.respond_to?(:csv_content=)
@task.csv_content = @run.csv_file.download
end

Or whether we wanted to introduce a class-level method like csv_task? instead. Let me know!

@adrianna-chang-shopify adrianna-chang-shopify requested review from volmer, etiennebarrie and rafaelfranca and removed request for volmer January 25, 2021 14:46
Copy link
Member

@etiennebarrie etiennebarrie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or whether we wanted to introduce a class-level method like csv_task? instead.

What I don't like about things like csv_task? is that now we have to define it in Task which should not care about CsvTask at all. I think respond_to? is fine.

app/tasks/maintenance_tasks/task.rb Outdated Show resolved Hide resolved
@adrianna-chang-shopify
Copy link
Contributor Author

What I don't like about things like csv_task? is that now we have to define it in Task which should not care about CsvTask at all

Yeah, that was my reasoning as well - I don't think the parent class should have to know anything about its subclasses and the specialized behaviour they provide. I'm keen on sticking to our use of respond_to?

@sambostock
Copy link
Contributor

If I'm not mistaken, .respond_to?(:csv_content=)/.csv_task? are used to trigger loading of the CSV contents in TaskJob, right?

if @task.respond_to?(:csv_content=)
  @task.csv_content = @run.csv_file.download
end

If that's it, would it make sense to define a generic hook/callback method on Task, which CsvTask overrides. That way you can invert things and make it so the TaskJob doesn't know anything about CSVs

# task.rb
class Task
  def before_perform(_run)
  end
end

# csv_task.rb
class CsvTask < Task
  def before_perform(run)
    self.csv_content = run.csv_file.download
    super
  end
end

This could alternatively be implemented as an ActiveSupport::Callback.

@etiennebarrie
Copy link
Member

would it make sense to define a generic hook/callback method on Task, which CsvTask overrides

It could make sense, but then we would give too much API to Task writers, which don't want to. We provide CsvTask and TaskJob, so I think even if not ideal, it's fine to have some knowledge between them.
Whereas for all the tasks that the users write, we don't want to give them control over the Run object, it's an implementation detail they shouldn't have to worry about.

@adrianna-chang-shopify
Copy link
Contributor Author

adrianna-chang-shopify commented Jan 25, 2021

Providing a callback on Task is pretty neat! But yeah, as Étienne mentioned, the tradeoff is making Task aware of Run, which we want to avoid. It's not the prettiest to use respond_to?, I agree, but since TaskJob already knows how to talk to Run and the Task instance it's creating, I believe it's favourable to having Tasks be aware of implementation details.

@adrianna-chang-shopify adrianna-chang-shopify merged commit 206ea5b into main Jan 25, 2021
@adrianna-chang-shopify adrianna-chang-shopify deleted the use-superclass-for-csv branch January 25, 2021 18:37
@adrianna-chang-shopify adrianna-chang-shopify added the CSV Requirements for CSV feature label Feb 11, 2021
@adrianna-chang-shopify adrianna-chang-shopify temporarily deployed to rubygems February 11, 2021 21:05 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CSV Requirements for CSV feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants