Class: Gourami::Form

Inherits:
Object
  • Object
show all
Includes:
Attributes, Coercer, Validations
Defined in:
lib/gourami/form.rb

Overview

Base Form for doing actions based on the attributes specified. This class has to be inherited by different forms, each performing a different action. If needed, #validate method can be overridden if necessary.

Examples:


class LogIn < Gourami::Form

  attribute(:username, :type => :string)
  attribute(:password, :type => :string)

  def validate
    unless valid_login?
      append_error(:username, :invalid_credentials)
    end
  end

  def perform
    User.create(attributes)
  end

  private

  def valid_login?
    user = User.first(:username => username)
    user && check_password_secure(user.password, password)
  end

end

class UpdateUser < Gourami::Form

  VALID_TYPES = %w[buyer seller]

  record(:user, :skip => true)
  attribute(:username, :type => :string)
  attribute(:first_name, :type => :string)
  attribute(:percentage, :type => :integer)
  attribute(:type, :type => :string)

  def validate
    validate_presence(:username)
    validate_length(:username, :min => 2, :max => 64)

    validate_presence(:first_name)

    validate_presence(:percantage)
    validate_range(:percentage, :min => 0, :max => 100)

    validate_presence(:type)
    validate_inclusion(:type, VALID_TYPES)
  end

  def perform
    user.update(attributes)
  end

end

# Usage of the new form class:

form = UpdateUser.new({
  :user => User.first,
  :username => "WeijieWorld",
  :first_name => "Weijie",
  :percentage => "100",
  :type => "buyer"
})

if form.valid?
  form.perform
  # Do something else
else
  # Do something else
end

Constant Summary

Constants included from FormattingConstants

Gourami::FormattingConstants::EMAIL_FORMAT, Gourami::FormattingConstants::HEX_COLOR_FORMAT, Gourami::FormattingConstants::ISRC_FORMAT

Class Method Summary collapse

Methods included from Coercer

#coerce_array, #coerce_boolean, #coerce_date, #coerce_file, #coerce_float, #coerce_hash, #coerce_integer, #coerce_phone, #coerce_string, #coerce_time, #setter_filter

Methods included from Validations

#any_errors?, #append_error, #append_root_error, #attribute_has_errors?, #clear_and_set_errors, #current_resource, #errors, #get_current_resource_attribute_value, #handle_validation_error, #perform!, #raise_validate_errors, #valid?, #validate, #validate_any, #validate_color_format, #validate_decimal_places, #validate_email_format, #validate_filetype, #validate_format, #validate_inclusion, #validate_inclusion_of_each, #validate_isrc_format, #validate_length, #validate_presence, #validate_range, #validate_uniqueness

Methods included from Attributes

#all_attributes, #attribute_provided?, #attributes, #attributes_hash_from_attributes_options, included, #initialize, #provided_attributes, #provided_attributes_names, #set_attributes, #setter_filter

Class Method Details

.plugin(name, *args, &block) ⇒ Object

Load and apply a plugin. Inspired by the plugin system from the Sequel gem. https://janko.io/the-plugin-system-of-sequel-and-roda/

Parameters:

  • name (Symbol, Module)

    a name to look up under Gourami::Plugins (e.g. :sanitize loads gourami/plugins/sanitize and resolves Gourami::Plugins::Sanitize), or a Module to apply directly.



97
98
99
100
101
102
103
104
105
106
# File 'lib/gourami/form.rb', line 97

def self.plugin(name, *args, &block)
  plugin_module = load_plugin(name)

  plugin_module.apply(self, *args, &block) if plugin_module.respond_to?(:apply)

  extend(plugin_module::ClassMethods) if plugin_module.const_defined?(:ClassMethods)
  include(plugin_module::InstanceMethods) if plugin_module.const_defined?(:InstanceMethods)

  plugin_module.configure(self, *args, &block) if plugin_module.respond_to?(:configure)
end