Git Product home page Git Product logo

ddc's Introduction

DDC

DDC (Data Driven Controllers) lets you declare how to wire Rails into your app without the need for code. A Rails controller's job is parsing/interpreting parameters to send to your application domain and taking those results and translating them back out to an HTTP result (html/status/headers). DDC removes the need for all the boiler plate controller code and tests.

By adhering to a couple of interfaces, you can avoid writing most controller code and tests. See this blog post for more information.

Installation

Add this line to your application's Gemfile:

gem 'ddc'

And then execute:

$ bundle

Or install it yourself as:

$ gem install ddc

Usage

Controllers

controllers/monkeys_controller.rb

DDC::ControllerBuilder.build :monkeys,
  before_actions: [:authenticate_user!],
  actions: {
    show: {
      context: 'context_builder#user_and_id',
      service: 'monkey_service#find'
    },
    index: {
      context: 'context_builder#user',
      service: 'monkey_service#find_all'
    },
    update: {
      context: 'context_builder#monkey',
      service: 'monkey_service#update'
    },
    create: {
      context: 'context_builder#monkey',
      service: 'monkey_service#create'
    }
  }

Context Builders

lib/context_builder.rb

class ContextBuilder
  def user(context_params)
    HashWithIndifferentAccess.new current_user: context_params[:current_user] 
  end

  def user_and_id(context_params)
    user(context_params).merge(id: context_params[:params][:id])
  end

  def monkey(context_params)
    info = context_params[:params].permit(monkey: [:color, :poo])
    user_and_id(context_params).merge(info)
  end
end

Services

lib/monkeys_service.rb

class MonkeyService
  def find(context)
    id, user = context.values_at :id, :current_user
    me = find_for_user user, id
    if me.present?
      ok(me)
    else
      not_found
    end
  end

  def update(context)
    id, user, updates = context.values_at :id, :current_user, @model_type
    me = find_for_user user, id

    translated_updates = translated_cid_to_id(updates)
    
    if me.present?
      me.update_attributes translated_updates
      ok(me)
    else
      not_found
    end
  end

  private
  def not_found
    {status: :not_found}.freeze
  end
  def ok(obj)
    {status: :ok, object: obj}
  end
end

# shortcut for default CRUD service
MonkeyService = DDC::ServiceBuilder.build(:monkey)

Contributing

  1. Fork it ( https://github.com/atomicobject/ddc/fork )
  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

ddc's People

Contributors

shawn42 avatar davidcelis avatar englishm avatar sleewoo avatar

Watchers

James Cloos 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.