Skip to content

Instantly share code, notes, and snippets.

@kubakrzempek
Created April 21, 2016 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kubakrzempek/f6c15643b9adae7e3de27a6b946aee30 to your computer and use it in GitHub Desktop.
Save kubakrzempek/f6c15643b9adae7e3de27a6b946aee30 to your computer and use it in GitHub Desktop.
module MarketForms
Create = Dry::Validation.Form do
key(:code).required
key(:name).required
key(:countries_id).required
key(:description).required
key(:organization_id).required
key(:active).required
rule(code_organization_uniq: [:code, :organization_id]) do |code, organization_id|
unique?(code, organization_id)
end
configure do
option :market_repo, WheelsApp.instance["repositories.market"]
def unique?(organization_id, code)
market_repo
.markets
.by_org(organization_id)
.where(code: code)
.one
.blank?
end
end
end
end
@AMHOL
Copy link

AMHOL commented Apr 21, 2016

Does this work?

module MarketForms
  Create = Dry::Validation.Form do
    key(:code).required
    key(:name).required
    key(:countries_id).required
    key(:description).required
    key(:organization_id).required
    key(:active).required

    rule(code_organization_uniq: [:code, :organization_id]) do |code, organization_id|
      code.unique_by_organisation_id?(organization_id)
    end

    configure do
      option :market_repo, WheelsApp.instance["repositories.market"]

      def unique_by_organisation_id?(organization_id, code)
        market_repo
          .markets
          .by_org(organization_id)
          .where(code: code)
          .one
          .blank?
      end
    end
  end
end

@kubakrzempek
Copy link
Author

Works perfectly! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment