Giter Site home page Giter Site logo

cruddler's Introduction

Cruddler – CRUD admin controllers for lazy programmers

NOTE: This is currently in in-house-testing-and-developing state. Feel free to use, but don’t complain if it doesn’t fit your needs. Feedback is welcome, though.

Mission Objective

I was sick of implementing edit, show, index, … actions over and over again, and wasn’t too enthusiastic about resource_controller and the like for they are way to overengineered.

The same annoyance I found in implementing the corresponding view.

And now, Rails 3.1 is there with view inheritance.

Installaton

add to your Gemfile:

  gem 'cruddler'

and install the view-inheritance views:

  rails g cruddler:install

This installs the index, show, new, edit files to app/views/admin/application/, if you want to use them elsewhere, just move them around as you like :)

It also installs a cruddler.yml locale file as described below in i18n.

Controller

just write

  cruddler :all

into your controller, or replace :all with a list of the actions you want to generate.

Options

  • :klass => the resource class, if it can’t be guessed from the controller name
  • :parameter_name => the name of the params entry, also used as the name of the instance variable generated
  • :nested => if the current resource is nested, either give the name of the nesting model, an array of the names of nesting models, or a hash {:model1 => Model1, :model2 => Model2, …} if the name/model connection can’t be guessed.
  • :nested_as => the name of the current resource inside the nesting model, if it can’t be guessed. Usually necessary for polymorphic nestings
  • :path_components => an array of path components like [:admin, :catalog] if they can’t be guessed from the controller name
  • :authorize => true if cancan authorize! is to be called for the current resource
  • :index_path => either a string, a symbol (:index, :show, :edit, :new) or a proc to declare the index path for redirects
  • :after_update_path => either a string, a symbol (:index, :show, :edit, :new) or a proc to declare where to redirect after updates
  • :after_create_path => either a string, a symbol (:index, :show, :edit, :new) or a proc to declare where to redirect after create
  • :after_destroy_path => either a string, a symbol (:index, :show, :edit, :new) or a proc to declare where to redirect after destroy
  • :stateful_index => if tabulatr is available, the find_for_table call geht the :stateful => session option.

We intentionally don’t offer any callbacks or the like, for typical CRUD actions are sufficiently simple to just implement them if they are non-standard. So — It you’re not happy with what cruddler generated, just implement your action ‘by hand’.

BTW: If tabulatr is available, the generated index action uses find_for_table instead of find.

Strong Parameters

To use strong parameters in rails 4, you have the following options:

Give block to generate house_parameters method

  class HousesController < ApplicationController
    cruddler :all do
      params.required(:house).permit(:name, :number)
    end
  end

Give permit_params option to implicitly define cat_params method:

params.required(:cat).permit(:name)

  class CatsController < ApplicationController
    cruddler :all,
      nested: 'house',
      index_path: ->(){ house_cats_path(@house) },
      permit_params: [:name]
  end

Give permit_params: :all option to implicitly define cat_params method:

params.required(:parasite).permit!

  class ParasitesController < ApplicationController
    cruddler :all,
      nested: ['house', 'cat'],
      index_path: ->(){ house_cat_parasites_path(@house, @cat) },
      permit_params: :all
  end

Give permit_params: →(){ closure } option to implicitly define dynamic cat_params method:

params.required(:dog).permit(<evaluation of closure>)

  class DogsController < ApplicationController
    cruddler :all,
      nested: 'house',
      nested_as: 'ref',
      permit_params: ->(){ @house.id == 2 ? :name : [] }
  end

Dealing with non-standard queries

In the standard case, cruddler uses the plain FooBar model in the FooBarController.
If you want to change this behavior, override cruddler_find_on in your controller.
The example below uses the unscoped model:

def cruddler_find_on
  FooBar.unscoped
end

Views

Just implement model_name, and listing partials, the rest is done magically. If your edit or new actions need to be more specific, you can override with edit_model_name or newmodelname.

As with the controllers, we don’t offer fancy customization options. If you don’t like the views, go to the cruddler gem, copy the views into your project, and customize them to your needs.

I18n

In the views and flash-messages, we use i18n, here’s an example, you get the idea:

  en:
    cruddler:
      abort: "Abort"
      admin: # path elements if necessary
        product:
          edit: "Edit product '%{name}'"
          index: "All our Products"
          create: "Create new Product"
          new: "Create new Product"
          update_success: "Product updated"
          update_problem: "Product can't be saved"
          create_success: "Product created"
          create_problem: "Product can't be created"

Bugs

There may be lots of bugs in cruddler, although we do some testing (see spec/). So if you hunt them, please let me know using the GitHub Bugtracker.

Contributing to cruddler

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet
  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it
  • Fork the project
  • Start a feature/bugfix branch
  • Commit and push until you are happy with your contribution
  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
  • Feel free to send a pull request if you think others (me, for example) would like to have your change incorporated into future versions of cruddler.

License

Copyright © 2010-2012 Peter Horn, metaminded UG

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.