Class: ActiveForm

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/active_form.rb

Overview

ActiveForm - non persistent ActiveRecord

Simple base class to make AR objects without a corresponding database table. These objects can still use AR validations but can’t be saved to the database.

Example

class FeedbackForm < ActiveForm
  column :email
  column :message, :type => :text
  validates_presence_of :email, :message
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.abstract_classObject

:nodoc:



44
45
46
# File 'lib/active_form.rb', line 44

def self.abstract_class # :nodoc:
  true
end

.column(name, options = {}) ⇒ Object

Define an attribute. It takes the following options:

:type

schema type

:default

default value

:null

whether it is nullable

:human_name

human readable name

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active_form.rb', line 25

def self.column(name, options = {})
  name = name.to_s
  options.each { |k,v| options[k] = v.to_s if Symbol === v }
  
  if human_name = options.delete(:human_name)
    name.instance_variable_set('@human_name', human_name)
    def name.humanize; @human_name; end
  end
  
  columns << ActiveRecord::ConnectionAdapters::Column.new(
    name,
    options.delete(:default),
    options.delete(:type),
    options.include?(:null) ? options.delete(:null) : true
  )
  
  raise ArgumentError.new("unknown option(s) #{options.inspect}") unless options.empty?
end

.columnsObject

:nodoc:



16
17
18
# File 'lib/active_form.rb', line 16

def self.columns # :nodoc:
  @columns ||= []
end

Instance Method Details

#saveObject

:nodoc:



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/active_form.rb', line 48

def save # :nodoc:
  if result = valid?
    callback(:before_save)
    callback(:before_create)
    
    # do nothing!
    
    callback(:after_save)
    callback(:after_create)
  end
  
  result
end

#save!Object

:nodoc:



62
63
64
# File 'lib/active_form.rb', line 62

def save! # :nodoc:
  save or raise ActiveRecord::RecordInvalid.new(self)
end