module Methods def crud_for(model, options) _options = { :create => true, :update => true, :destroy => true, :search => defined?(MetaSearch), :paginate => defined?(WillPaginate) } _options.merge!(options) expose(model.to_sym) if _options[:paginate] && _options[:search] expose(model.to_s.pluralize.to_sym) { search.paginate(:page => params[:page]) } else if _options[:paginate] expose(model.to_s.pluralize.to_sym) { model.to_s.capitalize.constantize.paginate(:page => params[:page]) } else if _options[:search] expose(model.to_s.pluralize.to_sym) { search.all } else expose(model.to_s.pluralize.to_sym) { model.to_s.capitalize.constantize.all } end end end expose(:search) { model.to_s.capitalize.constantize.search(params[:search]) } if _options[:search] self.class_eval %" def create if #{model}.save redirect_to #{model.to_s.pluralize}_url, I18n.t(:create_success, :model => #{model.to_s.capitalize.constantize}.human_name) else render 'new' end end " if _options[:create] self.class_eval %" def update if #{model}.update_attributes(params['#{model.to_sym}']) redirect_to #{model.to_s.pluralize}_url, I18n.t(:update_success, :model => #{model.to_s.capitalize.constantize}.human_name) else end end " if _options[:update] self.class_eval %" def destroy #{model}.destroy redirect_to #{model.to_s.pluralize}_url, I18n.t(:destroy_success, :model => #{model.to_s.capitalize.constantize}.human_name) end " if _options[:destroy] end end