Module: Conformista::Presenting

Included in:
FormObject
Defined in:
lib/conformista/presenting.rb

Overview

Presenting provides the macro to extend a form object with the methods to present models and to build, persist and validate them.

Given a model User, you can override the following methods to customise the default behaviour:

  • build_user: should return a new instance of the model
  • user: should return the currently presented model instance
  • user=: set the currently presented model instance
  • persist_user: persist the current model instance
  • load_user_attributes: get model attributes and store it in the form object
  • delegate_user_attributes: set model attributes from the form object attributes
  • validate_user: test if the model instance is valid

Examples:

Present a single model

class SignupForm < Conformista::FormObject
  presents User, :email, :password
end

Present multiple models

class SignupForm < Conformista::FormObject
  presents User    => %i[email password],
           Profile => %i[twitter github bio]
end

See Also:

Instance Method Summary collapse

Instance Method Details

#present_model(model, *attributes) ⇒ Object

Present a single model and its attributes

Parameters:

  • model (Class)

    the class of object to present

  • attributes (Symbol)

    one or more attribute names


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/conformista/presenting.rb', line 62

def present_model(model, *attributes)
  model_name = model.model_name.singular
  ivar = :"@#{model_name}".to_sym

  mod = Module.new do
    attr_accessor *attributes

    define_method :presented_models do
      super().tap do |orig|
        orig << model unless orig.include? model
      end
    end

    define_method model_name do
      if instance_variable_get(ivar).nil?
        instance_variable_set ivar, send(:"build_#{model_name}")
      else
        instance_variable_get ivar
      end
    end

    define_method :"build_#{model_name}" do
      model.new
    end

    define_method :"#{model_name}=" do |obj|
      instance_variable_set(ivar, obj).tap do |obj|
        send :"load_#{model_name}_attributes"
      end
    end

    define_method :"load_#{model_name}_attributes" do
      attributes.each do |attribute|
        send("#{attribute}=", send(model_name).send("#{attribute}"))
      end
    end

    define_method :"delegate_#{model_name}_attributes" do
      attributes.each do |attribute|
        send(model_name).send("#{attribute}=", send(attribute))
      end
    end

    define_method :"validate_#{model_name}" do
      send(model_name).valid?
    end

    define_method :"persist_#{model_name}" do
      send(model_name).save
    end
  end

  include mod
end

#present_models(options = {}) ⇒ Object

Present multiple models using a Hash of classes and attributes

Parameters:

  • options (Hash) (defaults to: {})

    models as keys, array of attributes as value


52
53
54
55
56
# File 'lib/conformista/presenting.rb', line 52

def present_models(options = {})
  options.each do |model, attributes|
    present_model model, *attributes
  end
end

#presents(model, *attributes) ⇒ Object #presents(models) ⇒ Object

Convenience method to use either present_models or present_model, based on the number of arguments passed in.

Overloads:

  • #presents(model, *attributes) ⇒ Object

    Present a single model and its attributes

    Parameters:

    • model (Class)
    • method (Symbol)

      name

  • #presents(models) ⇒ Object

    Present multiple models using a Hash of classes and attributes

    Parameters:

    • models (Hash)

      as keys, array of attributes as value


41
42
43
44
45
46
47
# File 'lib/conformista/presenting.rb', line 41

def presents(*args)
  if args.size == 1
    present_models *args
  else
    present_model *args
  end
end