Skip to content
This repository has been archived by the owner on Jun 11, 2022. It is now read-only.

define conditional attributes #63

Closed
tomkra opened this issue Jul 29, 2019 · 2 comments
Closed

define conditional attributes #63

tomkra opened this issue Jul 29, 2019 · 2 comments

Comments

@tomkra
Copy link

tomkra commented Jul 29, 2019

Hello,

is there any way to define conditional attributes? I need to do something like this:

class MyModel < ApplicationRecord
  include Storext.model

  enum model_type: { basic: 1, better: 2 }

  store_attributes :settings do
    setting_1 Boolean, default: false, if: proc { |m| m.basic? }
    setting_2 Boolean, default: false, if: proc { |m| m.better? }
  end
end

How can I achieve this? Thank you.

@ramontayag
Copy link
Contributor

There's none, but if you want that I think you should go for STI. You'll have a BasicModel with setting_1 and BetterModel with setting_2.

@tomkra
Copy link
Author

tomkra commented Aug 29, 2019

@ramontayag Thank you, this should solve my issue :).

Edit:
Another possible solution is this:

  store_attributes :settings do
    setting_1 Boolean, default: proc { |m| m.basic? ? false : nil }
    setting_2 Boolean, default: proc { |m| m.better? ? false :nil }
  end

This way it will use default value only for specific type.

@tomkra tomkra closed this as completed Aug 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants