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

Create interpret_date_assignment_for class method #6

Conversation

geshafer
Copy link
Contributor

@geshafer geshafer commented Jul 7, 2017

Our usage of the interpret_date gem has created a pattern of defining
several assignment methods with the same logic inside of them which is
not helpful in the model and can clutter them with enough dates defined.
This commit introduces a class method that is included with
InterpretDate that allows you to define the attributes which need
their assignment interpreted by interpret date so we can clean up the
models.

Old example:

class Payment < ApplicationRecord
  include InterpretDate

  def issued_date=(value)
    super(interpret_date(value) || value)
  end

  def paid_date=(value)
    super(interpret_date(value) || value)
  end

  def sent_date=(value)
    super(interpret_date(value) || value)
  end

  def voided_date=(value)
    super(interpret_date(value) || value)
  end
end

New example:

class Payment < ApplicationRecord
  include InterpretDate

  interpret_date_assignment_for :issued_date,
    :paid_date,
    :sent_date,
    :voided_date,
end

Our usage of the interpret_date gem has created a pattern of defining
several assignment methods with the same logic inside of them which is
not helpful in the model and can clutter them with enough dates defined.
This commit introduces a class method that is included with
`InterpretDate` that allows you to define the attributes which need
their assignment interpreted by interpret date so we can clean up the
models.

[380933786845645](https://app.asana.com/0/380933786845645/380933786845645)
end

module ClassMethods
def interpret_date_assignment_for(*attributes)
Copy link
Member

Choose a reason for hiding this comment

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

What do you think about shortening this method name? Some possible alternatives:

interpret_date_for
attr_interpret_date
interpret_date_attr
attr_writer_interpret_date
attr_interpret_date_writer

I like the first one in the list, the others are just thoughts.

@keiththomps
Copy link
Contributor

keiththomps commented Jul 7, 2017

Did you consider defining an ActiveRecord::Type for Dates? Moving forward that will be the more "rails-y" way to achieve this.

@@ -74,4 +74,32 @@
end
end
end

describe "#interpret_date_assignment_for" do
Copy link
Contributor

Choose a reason for hiding this comment

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

Class methods are annotated with a . instead of the # used for instance methods.

@geshafer geshafer closed this Jul 28, 2017
@geshafer geshafer deleted the 380933786845645-create-interpret-date-assignment-for-class-method branch July 28, 2017 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants