Module: ActionController::FlexRestScaffolding::ClassMethods

Defined in:
lib/rest_scaffolding.rb

Overview

REST Scaffolding provides Flex REST actions for creating, reading, updating, and destroying records.

Also returns the schema for the record

Example:

class ContactsController < ActionController::Base
  flex_scaffold :contacts
end

Instance Method Summary collapse

Instance Method Details

#flex_scaffold(model_id, options = {}) ⇒ Object

Adds Flex REST actions to the controller. The options are the same as for ActionView::Scaffolding.scaffold, except that the :suffix option is not currently supported.

These are merely the same as a resource albeit a couple of mods



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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/rest_scaffolding.rb', line 27

def flex_scaffold(model_id, options = {})
  options.assert_valid_keys(:class_name, :suffix, :size)
  
  swf_size      = options[:size] || FlexScaffold::Config.default_size
  singular_name = model_id.to_s
  class_name    = options[:class_name] || singular_name.camelize
  plural_name   = singular_name.pluralize
  
  class_eval <<-"end_eval", __FILE__, __LINE__
  # GET /contacts
  # GET /contacts.xml
  def index
    flex_index
  end
  
  def flex_index
    @#{singular_name} = #{class_name}.find(:all)
    @swf_name = "_#{plural_name}"
    @page_title = "#{plural_name.camelize}"
    @size = "#{swf_size}"

    respond_to do |format|
      format.html { render :template => "#{FlexScaffold::Config.plugin_name}/index" } # index.rhtml
      format.xml  { render :xml => @#{singular_name}.to_xml }
    end       
  end

  # GET /contacts/1
  # GET /contacts/1.xml
  def show
    @#{singular_name} = #{class_name}.find(params[:id])

    respond_to do |format|
      format.html # show.rhtml
      format.xml  { render :xml => @#{singular_name}.to_xml }
    end
  end

  # GET /contacts/new
  def new
    @#{singular_name} = #{class_name}.new
  end

  # GET /contacts/1;edit
  def edit
    @#{singular_name} = #{class_name}.find(params[:id])
  end

  # POST /contacts
  # POST /contacts.xml
  def create
    @#{singular_name} = #{class_name}.new(params[:#{singular_name}])

    respond_to do |format|
      if @#{singular_name}.save
        flash[:notice] = '#{class_name} was successfully created.'
        format.html { redirect_to contact_url(@#{singular_name}) }
        format.xml  { head :created } # :location => contact_url(@#{singular_name}) # removed from original
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @#{singular_name}.errors.to_xml }
      end
    end
  end

  # PUT /contacts/1
  # PUT /contacts/1.xml
  def update
    @#{singular_name} = #{class_name}.find(params[:id])

    respond_to do |format|
      if @#{singular_name}.update_attributes(params[:#{singular_name}])
        flash[:notice] = '#{class_name} was successfully updated.'
        format.html { redirect_to contact_url(@#{singular_name}) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @#{singular_name}.errors.to_xml }
      end
    end
  end

  # DELETE /contacts/1
  # DELETE /contacts/1.xml
  def destroy
    @#{singular_name} = #{class_name}.find(params[:id])
    @#{singular_name}.destroy

    respond_to do |format|
      format.html { redirect_to contacts_url }
      format.xml  { head :ok }
    end
  end
  
  # GET /contacts/schema/1
  # GET /contacts/schema/1.xml
  # TODO: changes routes so that this does not require an ID
  def schema
    response.headers["Content-Type"] = "text/xml"
    render :text => schema_xml(#{class_name})
  end
   
  end_eval
end