Class: AmosController

Inherits:
ApplicationController show all
Defined in:
app/controllers/amos_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#current_user

Instance Method Details

#createObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/controllers/amos_controller.rb', line 87

def create
  if can? :create, eval("#{@model}")
    p = remove_attributes_from ['id', 'model', 'controller', 'action'], params.clone 
    record = self.instance_eval("#{@model}.new(p)")

    if record.save
        result = filter_record record
        render :json => result
    else
        render :json => record.errors, :status => 400
    end
  else
    render_authorized
  end
end

#destroyObject



78
79
80
81
82
83
84
85
# File 'app/controllers/amos_controller.rb', line 78

def destroy
 if can? :delete, @record
   @record.destroy
   render :json => {:success => "true"}
 else
   render_authorized
 end
end

#findObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/amos_controller.rb', line 32

def find
  @the_fields = process_field_names([], params[:fields])
  terms = params[:term].split(',').collect{|t| "'#{t}'"}.join(',')
  
  if @paginate_flag
    records = eval("#{@model}.scoped_#{params[:query]}(#{terms})").paginate(:page => params[:page], :per_page => ActiveRecord::Base.per_page)
  else
    query = "#{@model}.find_#{params[:query]}(:all, #{terms})"
    records = self.instance_eval("#{@model}.find_all_#{params[:query]}(#{terms})")
  end
  records = [] if records.nil?

  result_records = []
  records.each{|rec|
    if @the_fields.count == 0
      result = filter_record rec
    else
      result = select_fields rec, @the_fields
    end
    result_records << result
  } unless records.nil?
  render :json => result_records 
end

#indexObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/amos_controller.rb', line 12

def index
  @the_fields = process_field_names([], params[:fields])
  if @paginate_flag
    records = self.instance_eval("#{@model}.paginate(:page => params[:page], :per_page => ActiveRecord::Base.per_page)")
  else
    records = self.instance_eval("#{@model}.all")
  end
  
  result_records = []
  records.each{|rec|
    if @the_fields.count == 0
      result = filter_record rec
    else
      result = select_fields rec, @the_fields
    end
    result_records << result
  } unless records.nil?
  render :json => result_records 
end

#showObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/amos_controller.rb', line 56

def show
 @the_fields = process_field_names([], params[:fields])
 if @the_fields.count == 0
   result = filter_record @record
 else
   result = select_fields @record, @the_fields
 end
 
 @the_associations = process_association_names([], params[:association])
 if @the_associations.count > 0
   @the_associations.each{|name|
     assoc_records = self.instance_eval("@record.#{name}")
     data = []
     assoc_records.each{|ar|
       data << filter_record(ar, ["#{@model.downcase}_id"])
     }
     result[name] = data
   }
 end
 render :json => result
end

#updateObject



103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/amos_controller.rb', line 103

def update
  if can? :update, eval("#{@model}")
    attributes = remove_attributes_from ['id', 'model', 'controller', 'action'], params.clone 
    if @record.update_attributes(attributes)
      render :json => attributes
    else
      render :json => @record.errors, :status => 400
    end
  else
    render_authorized
  end
end