Module: DaoHelper

Defined in:
lib/dao/rails/lib/generators/dao/templates/dao_helper.rb

Instance Method Summary collapse

Instance Method Details

#dao(path, *args, &block) ⇒ Object


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dao/rails/lib/generators/dao/templates/dao_helper.rb', line 79

def dao(path, *args, &block)
  options = args.extract_options!.to_options!

  mode = options[:mode]

  if mode.blank?
    mode =
      case request.method
        when "GET"
          :read
        when "PUT", "POST", "DELETE"
          :write
        else
          :read
      end
  end

  @dao = api.send(mode, path, params)
  @dao.route = request.fullpath

  unless options[:error!] == false
    @dao.error! unless(@dao.status =~ 200 or @dao.status == 420)
  end

  block ? block.call(@dao) : @dao
end

#dao_form_attrs(*args) ⇒ Object


57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/dao/rails/lib/generators/dao/templates/dao_helper.rb', line 57

def dao_form_attrs(*args)
  args.flatten!

  options = args.extract_options!.to_options!.dup

  options[:class] =
    [args, options.delete(:class)].join(' ').scan(%r/[^\s]+/).push(' dao ').uniq.join(' ')

  options[:enctype] ||= "multipart/form-data"

  options
end

#dao_form_for(*args, &block) ⇒ Object Also known as: dao_form


3
4
5
6
7
8
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dao/rails/lib/generators/dao/templates/dao_helper.rb', line 3

def dao_form_for(*args, &block)
# grok the model, or build one on the fly
#
  model = args.flatten.select{|arg| arg.respond_to?(:persisted?)}.last

  options = args.extract_options!.to_options!

  args.push(options)

  if model.blank?
    name = 'form'
    model = Class.new(Dao::Conducer){ model_name(name) }.new(params[name])
    args.unshift(model)
  end

# build urls to *relative to the current controller* (with respect to the
# resource's state) unless specified...
#
  html = dao_form_attrs(options.delete(:html) || {})

  if model
    url = options.delete(:url)
    method = options.delete(:method) || html.delete(:method)

    if model.persisted?
      method ||= :put
    else
      method ||= :post
    end

    url ||=
      case method
        when /post/
          url_for(:action => :create)
        when /put/
          url_for(:action => :update)
        else
          './'
      end

    options[:url] = url
    options[:html] = html.merge(:method => method)
  end

# use a dao form builder...
#
  options[:builder] = Dao::Form::Builder

# delegate the rest of th magick to rails...
#
  form_for(*args, &block)
end

#render_dao(result, *args, &block) ⇒ Object


70
71
72
73
74
75
76
77
# File 'lib/dao/rails/lib/generators/dao/templates/dao_helper.rb', line 70

def render_dao(result, *args, &block)
  if result.status =~ 200 or result.status == 420
    @result = result unless defined?(@result)
    render(*args, &block)
  else
    result.error!
  end
end