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

Adding before_validation and and after_validation hooks #63

Open
arushs opened this issue Aug 12, 2020 · 2 comments
Open

Adding before_validation and and after_validation hooks #63

arushs opened this issue Aug 12, 2020 · 2 comments
Labels
help wanted Extra attention is needed

Comments

@arushs
Copy link

arushs commented Aug 12, 2020

First of all, StoreModel is awesome and we've been using it more and more in my teams project. We've been using it mostly for config type of data but I'm experimenting with using it for polymorphic records stored on a ActiveRecord model. One thing I think would be helpful (but maybe out of scope of this gem) is implementing before_validation and and after_validation hooks.

This could be useful in a number of ways. We could use before_validation to auto set values of fields. E.g. if we want to set randomly generated token as a unique identifier.

class FooStoreModel 
  include StoreModel::Model
  attribute :token, :string

  before_validation :set_token, on: :create


  private
    def set_token
      token = SecureRandom.uuid
    end
end

Another way this could be useful is for replicating STI type of inheritance. The polymorphic typing is awesome and I think it should conform to the Rails way of polymorphic STI (using a type variable).

class AbstractStoreModel 
  include StoreModel::Model
  attribute :type, :string

  before_validation :set_type, on: :create

  private
    def set_type
      type= self.class.name
    end
end

class FooStoreModel < AbstractStoreModel
end

class BarStoreModel < AbstractStoreModel
  attribute :value, :string
end


class FooRecord < ActiveRecord::Base
  StoreModelsType = StoreModel.one_of do |json|
    case json['type']
    when 'FooStoreModel'
      FooStoreModel
    when 'BarStoreModel'
      BarStoreModel
    else
      raise InvalidTypeError
    end
  end

  attribute :data, StoreModelsType.to_array_type
end

This would allow something like

  record = FooRecord.create!
  record.data << BarStoreModel.new(value: 'baz')
  record.save

which would auto cast and infer types

@DmitryTsepelev
Copy link
Owner

Hi @arushs, thanks for the great idea! I guess it will be a bit tricky, since before_validation is defined inside ActiveRecord::Callbacks module, and we do not need all these callbacks.

@DmitryTsepelev DmitryTsepelev added the help wanted Extra attention is needed label Aug 17, 2020
@adamzapasnik
Copy link

I think you can include ActiveModel::Validations::Callbacks https://api.rubyonrails.org/classes/ActiveModel/Validations/Callbacks.html and it should work correctly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants