Sha256: e725ebec700045759366cb1e33434ac86054edefc702e09682de9fb159193eb2

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 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.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

Version data entries

1 entries across 1 versions & 1 rubygems

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