GithubHelp home page GithubHelp logo

dbackowski / rails_simple_exposure Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 25 KB

Cleanup your Rails controllers.

License: MIT License

Ruby 77.38% JavaScript 3.62% CSS 2.35% HTML 16.65%
rails exposure controllers dry

rails_simple_exposure's Introduction

RailsSimpleExposure Build Status

Cleanup your Rails controllers.

Installation

Add this line to your application's Gemfile:

gem 'rails_simple_exposure'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rails_simple_exposure

Usage

Here's what a standard Rails CRUD controller using this plugin might look like:

class BooksController < ApplicationController
  expose :books, -> { Book.all }
  expose :book, -> { params[:id].present? ? Book.find(params[:id]) : Book.new }

  def create
    book.attributes = book_params

    respond_to do |format|
      if book.save
        format.html { redirect_to book, notice: 'Book was successfully created.' }
        format.json { render :show, status: :created, location: book }
      else
        format.html { render :new }
        format.json { render json: book.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if book.update(book_params)
        format.html { redirect_to book, notice: 'Book was successfully updated.' }
        format.json { render :show, status: :ok, location: book }
      else
        format.html { render :edit }
        format.json { render json: book.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    book.destroy
    respond_to do |format|
      format.html { redirect_to books_url, notice: 'Book was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
  def book_params
    params.require(:book).permit(:title)
  end
end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

License

The gem is available as open source under the terms of the MIT License.

rails_simple_exposure's People

Contributors

dbackowski avatar

Watchers

 avatar  avatar

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.