Class: Crud::CrudController

Inherits:
CrudBaseController show all
Defined in:
app/controllers/crud/crud_controller.rb

Instance Method Summary collapse

Methods inherited from CrudBaseController

#is_allowed_to_update?, #is_allowed_to_view?

Instance Method Details

#createObject

POST



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/crud/crud_controller.rb', line 79

def create
  @klass_data = @klass_info[:class].new(klass_params)

  respond_to do |format|
    if @klass_data.save
      format.html { redirect_to show_path(:class_name => @klass_info[:name], :id => @klass_data.id), :notice => "#{@klass_info[:name]} was successfully created" }
      format.json { render :json => @klass_data, :status => :created, :location => @klass_data }
      format.xml  { head :ok }
    else
      format.html { render :action => 'new' }
      format.json { render :json => @klass_data.errors, :status => :unprocessable_entity }
      format.xml  { render :xml => @klass_data.errors, :status => :unprocessable_entity }
    end
  end
end

#deleteObject

DELETE



111
112
113
114
115
116
117
118
119
# File 'app/controllers/crud/crud_controller.rb', line 111

def delete
  @klass_data.delete

  respond_to do |format|
    format.html { redirect_to index_path(:class_name => @klass_info[:name]), :notice => "#{@klass_info[:name]}[#{params[:id]}] was successfully deleted" }
    format.json { head :no_content }
    format.xml  { head :ok }
  end
end

#destroyObject

DELETE



122
123
124
125
126
127
128
129
130
# File 'app/controllers/crud/crud_controller.rb', line 122

def destroy
  @klass_data.destroy

  respond_to do |format|
    format.html { redirect_to index_path(:class_name => @klass_info[:name]), :notice => "#{@klass_info[:name]}[#{params[:id]}] was successfully destroyed" }
    format.json { head :no_content }
    format.xml  { head :ok }
  end
end

#editObject

GET



75
76
# File 'app/controllers/crud/crud_controller.rb', line 75

def edit
end

#indexObject

GET



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
# File 'app/controllers/crud/crud_controller.rb', line 23

def index
  @search_prompt = I18n.t(:search_prompt)

  page = (params[:page]) ? params[:page].to_i : 1
  per_page = (params[:per_page]) ? params[:per_page].to_i : 10

  if( params[:search] && !params[:search].empty? && params[:search] != @search_prompt)
    where_clause = ''
    @visible_attributes.each do |attribute|
      where_clause += "LOWER(#{attribute[:column_name]}) LIKE '%#{params[:search].downcase}%' OR " if attribute[:column_data_type] == :string
    end
    where_clause.sub!(/ OR $/,'')
  else
    where_clause = nil
  end

  primary_key_name = @klass_info.primary_key(@klass_info[:class])
  if primary_key_name
    @klass_data = @klass_info[:class].where(where_clause).paginate(:page => page, :per_page => per_page).order("#{primary_key_name} ASC")
  else
    @klass_data = @klass_info[:class].where(where_clause).paginate(:page => page, :per_page => per_page)
  end

  respond_to do |format|
    format.html # index.html.erb
    format.js   # index.js.erb
    format.json { render :json => @klass_data }
    format.xml  { render :xml => @klass_data }
  end
end

#newObject

GET



64
65
66
67
68
69
70
71
72
# File 'app/controllers/crud/crud_controller.rb', line 64

def new
  @klass_data = @klass_info[:class].new

  respond_to do |format|
    format.html # index.html.erb
    format.json { render :json => @klass_data }
    format.xml  { render :xml => @klass_data }
  end
end

#showObject

GET



55
56
57
58
59
60
61
# File 'app/controllers/crud/crud_controller.rb', line 55

def show
  respond_to do |format|
    format.html # index.html.erb
    format.json { render :json => @klass_data }
    format.xml  { render :xml => @klass_data }
  end
end

#updateObject

PUT



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/controllers/crud/crud_controller.rb', line 96

def update
  respond_to do |format|
    if @klass_data.update_attributes(klass_params)
      format.html { redirect_to show_path(:class_name => @klass_info[:name], :id => @klass_data.id), :notice => "#{@klass_info[:name]} was successfully updated" }
      format.json { head :no_content }
      format.xml  { head :ok }
    else
      format.html { render :action => 'edit' }
      format.json { render :json => @klass_data.errors, :status => :unprocessable_entity }
      format.xml  { render :xml => @klass_data.errors, :status => :unprocessable_entity }
    end
  end
end