Class: DynaForm::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming, Constants
Includes:
ActiveModel::Conversion, ActiveModel::Validations
Defined in:
lib/dyna_form/base.rb

Constant Summary

Constants included from Constants

Constants::SUBMIT_HASH_LIMIT

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Base

Returns a new instance of Base.



33
34
35
36
37
38
# File 'lib/dyna_form/base.rb', line 33

def initialize(args = {})
  @lazy_initialization = (args == {})
  args.each do |key, val|
    create_var(key, val)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments, &block) ⇒ Object



40
41
42
# File 'lib/dyna_form/base.rb', line 40

def method_missing(method, *arguments, &block)
  @lazy_initialization ? create_var(method, arguments.first) : super
end

Class Method Details

.submit(*args, hash) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dyna_form/base.rb', line 10

def submit(*args, hash)
  updated_hsh = hash.with_indifferent_access
  unless updated_hsh.length == SUBMIT_HASH_LIMIT && updated_hsh.has_key?(:to)
    raise "Invalid parameter list." 
  end

  raw_model = updated_hsh[:to].to_s
  words = raw_model.split("_")
  model_name = words.map { |w| w[0..0].capitalize + w[1..-1] }.join("")
  
  fields =  begin
              class_variable_get(:@@fields)
            rescue NameError
              class_variable_set(:@@fields, {})
              {}
            end

  fields[model_name] ||= []
  fields[model_name] += args
  class_variable_set(:@@fields, fields)
end

Instance Method Details

#persisted?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/dyna_form/base.rb', line 52

def persisted?
  false
end

#submitObject



48
49
50
# File 'lib/dyna_form/base.rb', line 48

def submit
  submissions.each { |s| s.submit }
end

#submit!Object



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

def submit!
  submissions.each { |s| s.submit! }
end