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

Method for working with VirtualForms more easily #69

Closed
paulcsmith opened this issue Mar 2, 2019 · 1 comment · Fixed by #469
Closed

Method for working with VirtualForms more easily #69

paulcsmith opened this issue Mar 2, 2019 · 1 comment · Fixed by #469

Comments

@paulcsmith
Copy link
Member

paulcsmith commented Mar 2, 2019

It is a common pattern when using VirtualForms to yield the form object and one or more additional objects when the form is valid. For example:

class MyForm < Avram::VirtualForm
  virtual some_field : String

  def submit
    validate_required some_field

    if valid?
      # Only yield the user if form is valid
      yield self, @user
    else
      yield self, nil
    end
  end
end

# Used like this
MyForm.submit do |form, user|
  if user
    # Do something
  else 
    # Do something else
  end
end

This can get repetitive and error-prone.

Another real-life example is here:
https://github.com/luckyframework/lucky_cli/pull/312/files#diff-b6e4ba970ba3c90d012a7fded94d57b2

Proposed solution

From comment here: #69 (comment)

class SearchUsers < Avram::VirtualOperation
  attribute query : String
  step { validate_size query, min: 2 }
  
  def result
    if valid? # If no validation errors on attributes
      UserQuery.search(query.value)
    else
      UserQuery.new.none
    end
  end
end

SearchUsers.run(params) do |operation, results|
  # do something
end

An example of the RequestResetPassword operation. Original here (https://github.com/luckyframework/lucky_cli/blob/d78429c8e40f97af20437cb8199de51c4962159f/src/base_authentication_app_skeleton/src/operations/request_password_reset.cr#L1)

class RequestPasswordReset < Avram::VirtualOperation
  # You can modify this in src/operations/mixins/user_from_email.cr
  include UserFromEmail
  step validate_email

  attribute email : String

  def result
    user_from_email
  end

  def validate_email
    validate_required email
    if user_from_email?
      email.add_error "is not in our system"
    end
  end
end
@paulcsmith
Copy link
Member Author

paulcsmith commented Jul 12, 2019

Further ideas: https://gist.github.com/paulcsmith/9eece27711c512751bdac03976b1bc42

class SearchUsers < Avram::VirtualOperation
  attribute query : String
  step { validate_size query, min: 2 }
  
  def result
    if valid? # If no validation errors on attributes
      UserQuery.search(query.value)
    else
      UserQuery.new.none
    end
  end
end

SearchUsers.run(params) do |operation, results|
  # do something
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant