Class: StudyEngine::Form

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Validations
Defined in:
app/models/study_engine/form.rb

Direct Known Subclasses

BeginAssessmentForm

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, model = self.class.wrapped_class.new) ⇒ Form

Returns a new instance of Form.



38
39
40
41
42
43
# File 'app/models/study_engine/form.rb', line 38

def initialize attributes = {}, model = self.class.wrapped_class.new
  @model = model
  attributes.each do |name, value|
    send("#{name}=", value)
  end
end

Class Method Details

.attributes(*attributes) ⇒ Object



26
27
28
29
30
31
# File 'app/models/study_engine/form.rb', line 26

def self.attributes *attributes
  attributes.each do |attribute|
    attributes_list << attribute
    attr_accessor attribute
  end
end

.lookup_ancestorsObject



20
21
22
# File 'app/models/study_engine/form.rb', line 20

def self.lookup_ancestors
  [self]
end

.model_nameObject



12
13
14
# File 'app/models/study_engine/form.rb', line 12

def self.model_name
  ActiveModel::Name.new(wrapped_class)
end

.new_from_model(model) ⇒ Object



33
34
35
36
# File 'app/models/study_engine/form.rb', line 33

def self.new_from_model model
  attributes = model.attributes.slice(*attributes_list.map(&:to_s))
  new attributes
end

.wrapped_classObject



16
17
18
# File 'app/models/study_engine/form.rb', line 16

def self.wrapped_class
  @wraps.to_s.classify.constantize
end

.wraps(model) ⇒ Object



8
9
10
# File 'app/models/study_engine/form.rb', line 8

def self.wraps(model)
  @wraps = model
end

Instance Method Details

#attributesObject



45
46
47
48
49
50
# File 'app/models/study_engine/form.rb', line 45

def attributes
  attributes_list.reduce({}) do |hash, key|
    hash[key] = send(key)
    hash
  end
end

#saveObject



52
53
54
# File 'app/models/study_engine/form.rb', line 52

def save
  @model.update_attributes(attributes) if valid?
end