Sha256: cfc0e4cb5c7e29f424884b56fe768bbc026948c57ff75bc7937de44ae45ce321

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

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.camelize.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.camelize.constantize.all }
        end
      end
    end
    
    expose(:search) { model.to_s.camelize.constantize.search(params[:search]) } if _options[:search]
      
    self.class_eval %"
      def create
        if #{model}.save
          redirect_to #{model.to_s.pluralize}_url, :notice => I18n.t(:create_success, :model => #{model.to_s.camelize.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, :notice => I18n.t(:update_success, :model => #{model.to_s.camelize.constantize}.human_name)
        else
          render 'edit'
        end
      end
    " if _options[:update]
    
    self.class_eval %"
      def destroy
        #{model}.destroy
        redirect_to #{model.to_s.pluralize}_url, :notice => I18n.t(:destroy_success, :model => #{model.to_s.camelize.constantize}.human_name)
      end
    " if _options[:destroy]
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
crud_for-0.0.2 lib/crud_for/methods.rb