Module: CrudController::ClassMethods

Defined in:
lib/crudcontroller.rb

Instance Method Summary collapse

Instance Method Details

#generate_for(model) ⇒ 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/crudcontroller.rb', line 9

def generate_for(model)
  model = model.to_s
  klass_name = model.camelize
  plural  = model.pluralize

  class_eval %Q!

    before_filter :load_page, :load_conditions, :only => :index
    before_filter :load_#{model}, :only => [ :edit, :new, :create, :update, :destroy ]

    def index
      load_records
      respond_to do |format|
        format.html
        format.json { render :json => { :results => @#{plural},
                                        :total => @#{plural}.total_entries
                                       }.to_json( to_json_options )
                    }
      end
    end

    def show
      @#{model} = #{klass_name}.find(params[:id])
    end

    def edit
      on_edit( @#{model} )
      render :edit
    end

    alias :new :edit

    def create
      create_or_update
    end

    def update
      create_or_update
    end

    def destroy
      @#{model}.destroy
      respond_to do |format|
        format.json  { render :json => {
                                         :success => 'true'
                                       }
                     }
      end
    end

    protected

    def load_records
      @#{plural} = paginate( #{klass_name}, (paginate_options.merge({:conditions => @conditions})) )
    end

    def paginate_options
      {}
    end

    def to_json_options
      {}
    end

    def on_edit(record)
    end

    def on_create_or_update(record)
    end

    def load_#{model}
      @#{model} = params[:id].blank? ? #{klass_name}.new : #{klass_name}.find(params[:id])
    end

    def paginate( #{model}, options = {} )
      #{model}.paginate( { :page => @page, :per_page => @per_page }.merge(options) )
    end

    def load_page
      @page = params[:page] || '1'
      @per_page = (params[:limit] || '100').to_i
      true
    end

    def create_or_update
      on_create_or_update( @#{model} )
      status = @#{model}.new_record? ? :created : :ok
      if @#{model}.update_attributes(params[:#{model}])
        respond_to do |format|
          format.html { redirect_to(@#{model}) }
          format.json  { render :json => {
                                           :success => 'true',
                                           :id => @#{model}.id,
                                           :record => @#{model}
                                         },
                                :status => status,
                                :location => @#{model}
                        }
        end
      else
        respond_to do |format|
          @errors = { :attributes => {}, :base => @#{model}.errors.on_base}

          @#{model}.errors.each do |attr, msg|
            @errors[:attributes]["#{model}_\#{attr}"]    ||= []
            @errors[:attributes]["#{model}_\#{attr}_id"] ||= []
            @errors[:attributes]["#{model}_\#{attr}"]    << msg
            @errors[:attributes]["#{model}_\#{attr}_id"] << msg
          end

          format.html { render :action => "new" }
          format.json  { render :json => { :success => 'false',
                                           :errors => @errors
                                         },
                                :status => :unprocessable_entity,
                                :location => @#{model}
                       }
        end
      end
    end

    def load_conditions
      fieldsForSearch = eval(params[:fields]) if params[:fields]


      query = params[:query] if params[:query]

      aditional_filter = params[:aditionalFilter] ? params[:aditionalFilter] : ''

      if (query == "")
        query = false
      end

      conditions_array = []

      if (fieldsForSearch && query)
        fieldsForSearch.each do |field|
          if (#{klass_name}.columns_hash[field].type == :string)
            conditions_array << \"UPPER(\#{field}) LIKE UPPER('%\#{query}%'\)"
          else
            if (query.to_i \!= 0)
              conditions_array << \"\#{field} = \#{query}\"
            end
          end
        end
      end

      conditions_array << \"\#{aditional_filter}\" unless aditional_filter.blank?

      @conditions = conditions_array.join(' OR ')
    end

  !

end