Class: Perus::Server::Form

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/perus/server/form.rb

Instance Method Summary collapse

Methods included from Helpers

#clean_arrows, #command_actions, #command_metrics, #escape_quotes, #load_site_information, #nav_item, #url_prefix

Constructor Details

#initialize(record) ⇒ Form

Returns a new instance of Form.



5
6
7
# File 'lib/perus/server/form.rb', line 5

def initialize(record)
    @record = record
end

Instance Method Details

#association(field, options) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/perus/server/form.rb', line 46

def association(field, options)
    reflection  = @record.class.association_reflections[field.to_sym]
    other_model = reflection[:class_name].constantize
    id_field    = reflection[:key]

    values = other_model.all.collect do |record|
        [record.id, record.name]
    end

    select(id_field, values)
end

#errors_for(field) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/perus/server/form.rb', line 68

def errors_for(field)
    errors = @record.errors.on(field.to_sym)
    return '' unless errors
    field_name = field.titlecase
    descriptions = errors.map {|error| "#{field_name} #{error}"}
    "<p class=\"errors\">#{descriptions.join(', ')}</p>"
end

#field(field, type = nil, options = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/perus/server/form.rb', line 9

def field(field, type = nil, options = nil)
    field = field.to_s

    if type.nil?
        if @record.class.association_reflections.include?(field.to_sym)
            type = 'association'
        else
            type = @record.db_schema[field.to_sym][:db_type]
        end
    end

    html = "<p><label for=\"#{field}\">#{field.titlecase}:</label><span>"

    case type
    when 'varchar(255)'
        html << input(field, options)
    when 'text'
        html << textarea(field, options)
    when 'association'
        html << association(field, options)
    when 'select'
        html << select(field, options)
    end

    # return the field plus any errors
    html << "</span></p>" << errors_for(field)
end

#input(field, options) ⇒ Object



37
38
39
40
# File 'lib/perus/server/form.rb', line 37

def input(field, options)
    value = escape_quotes(@record.send(field))
    "<input type=\"text\" name=\"record[#{field}]\" value=\"#{value}\">"
end

#select(field, options) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/perus/server/form.rb', line 58

def select(field, options)
    existing = @record.send(field)
    option_rows = options.collect do |(value, name)|
        selected = existing == value ? 'selected' : ''
        "<option value=\"#{escape_quotes(value)}\" #{selected}>#{name || value}</option>"
    end

    "<select name=\"record[#{field}]\">#{option_rows.join("\n")}</select>"
end

#textarea(field, options) ⇒ Object



42
43
44
# File 'lib/perus/server/form.rb', line 42

def textarea(field, options)
    "<textarea name=\"record[#{field}]\">#{@record.send(field)}</textarea>"
end