Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

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

This comment has been minimized.

Copy link
Owner Author

kubakrzempek commented Apr 21, 2016

Works perfectly! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.