Class: OpenRosa::Form

Inherits:
Object
  • Object
show all
Extended by:
FormDSL
Defined in:
lib/open_rosa/form.rb

Overview

Base class for defining OpenRosa forms with a clean DSL

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FormDSL

boolean, group, input, range, repeat, select, select1, trigger, upload

Class Method Details

.description_text(text = nil) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/open_rosa/form.rb', line 33

def description_text(text = nil)
  if text
    @description_text = text
  else
    @description_text
  end
end

.description_url(url = nil) ⇒ Object



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

def description_url(url = nil)
  if url
    @description_url = url
  else
    @description_url
  end
end

.download_url(url = nil) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/open_rosa/form.rb', line 49

def download_url(url = nil)
  if url
    @download_url = url
  else
    @download_url
  end
end

.form_hashObject

Calculates MD5 hash of the form's XForm XML content Returns format: "md5:abc123..."



67
68
69
70
71
72
# File 'lib/open_rosa/form.rb', line 67

def form_hash
  require "digest"
  xml = to_xml
  hash = Digest::MD5.hexdigest(xml)
  "md5:#{hash}"
end

.form_id(id = nil) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/open_rosa/form.rb', line 9

def form_id(id = nil)
  if id
    @form_id = id
  else
    @form_id
  end
end

.handle_submission(submission) ⇒ String?

Handle a submission (called by middleware)

Parameters:

  • submission (Submission)

    The parsed submission

Returns:

  • (String, nil)

    Success message or nil



101
102
103
# File 'lib/open_rosa/form.rb', line 101

def handle_submission(submission)
  @submission_handler&.call(submission)
end

.manifest_url(url = nil) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/open_rosa/form.rb', line 57

def manifest_url(url = nil)
  if url
    @manifest_url = url
  else
    @manifest_url
  end
end

.name(form_name = nil) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/open_rosa/form.rb', line 25

def name(form_name = nil)
  if form_name
    @name = form_name
  else
    @name
  end
end

.on_submit {|submission| ... } ⇒ Object

Define a handler for form submissions

Example:

class MyForm < OpenRosa::Form
on_submit do |submission|
  MyModel.create!(data: submission.data)
  "Thanks for submitting!"
end
end

Yields:

  • (submission)

    The parsed submission

Yield Parameters:

  • submission (Submission)

    The submission object

Yield Returns:

  • (String, nil)

    Optional success message



93
94
95
# File 'lib/open_rosa/form.rb', line 93

def on_submit(&block)
  @submission_handler = block
end

.to_xmlObject

Generates XForm XML from the form definition Returns the complete XForm XML as a string



76
77
78
# File 'lib/open_rosa/form.rb', line 76

def to_xml
  XForm.new(self).to_xml
end

.version(ver = nil) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/open_rosa/form.rb', line 17

def version(ver = nil)
  if ver
    @version = ver
  else
    @version
  end
end

Instance Method Details

#description_textObject



118
119
120
# File 'lib/open_rosa/form.rb', line 118

def description_text
  self.class.description_text
end

#description_urlObject



122
123
124
# File 'lib/open_rosa/form.rb', line 122

def description_url
  self.class.description_url
end

#download_urlObject



126
127
128
# File 'lib/open_rosa/form.rb', line 126

def download_url
  self.class.download_url
end

#fieldsObject



138
139
140
# File 'lib/open_rosa/form.rb', line 138

def fields
  self.class.fields
end

#form_hashObject



134
135
136
# File 'lib/open_rosa/form.rb', line 134

def form_hash
  self.class.form_hash
end

#form_idObject



106
107
108
# File 'lib/open_rosa/form.rb', line 106

def form_id
  self.class.form_id
end

#manifest_urlObject



130
131
132
# File 'lib/open_rosa/form.rb', line 130

def manifest_url
  self.class.manifest_url
end

#nameObject



114
115
116
# File 'lib/open_rosa/form.rb', line 114

def name
  self.class.name
end

#to_xmlObject



142
143
144
# File 'lib/open_rosa/form.rb', line 142

def to_xml
  self.class.to_xml
end

#versionObject



110
111
112
# File 'lib/open_rosa/form.rb', line 110

def version
  self.class.version
end