Giter VIP home page Giter VIP logo

chanko's Introduction

Chanko Build Status Code Climate Coverage Status

http://cookpad.github.io/chanko/

Chanko provides a simple framework for rapidly and safely prototyping new features in your production Rails app, and exposing these prototypes to specified segments of your user base.

With Chanko, you can release many features concurrently and manage target users independently. When any errors are raised from chanko's features, it will be automatically hidden and fallback to its normal behavior.

Requirements

  • Ruby >= 2.1.0
  • Rails >= 4.0.0

Usage

Add to your Gemfile.

gem "chanko"

Files

Chanko provides a generator to create templates of an unit.

$ rails generate chanko:unit example_unit
      create  app/units/example_unit
      create  app/units/example_unit/example_unit.rb
      create  app/units/example_unit/views/.gitkeep
      create  app/units/example_unit/images/.gitkeep
      create  app/units/example_unit/javascripts/.gitkeep
      create  app/units/example_unit/stylesheets/.gitkeep
      create  app/assets/images/units/example_unit
      create  app/assets/javascripts/units/example_unit
      create  app/assets/stylesheets/units/example_unit

Invoke

You can invoke the logics defined in your units via invoke and unit methods. In controller class context, unit_action utility is also provided. The block passed to invoke is a fallback executed if any problem occurs in invoking.

# app/controllers/users_controller.rb
class UsersController < ApplicationController
  unit_action :example_unit, :show

  def index
    invoke(:example_unit, :index) do
      @users = User.all
    end
  end
end
-# app/views/examples/index.html.slim
= unit.helper_method
= invoke(:example_unit, :render_example)
-# app/units/example_unit/views/_example.html.slim
= foo

Unit

You can see the real example of an unit module file.

module

You can define your MVC code here.

# app/units/example_unit/example_unit.rb
module ExampleUnit
  include Chanko::Unit
  ...
end

active_if

This block is used to decide if this unit is active or not. context is the receiver object of invoke. options is passed via invoke(:foo, :bar, :active_if_options => { ... }). By default, this is set as active_if { true }.

active_if do |context, options|
  true
end

raise_error

By default, any error raised in production env is ignored. raise_error is used to force an unit to raise up errors occured in invoking. You can force all units to raise up errors by Config.raise_error = true.

raise_error

function

In controller or view context, you can call functions defined by function via invoke(:example_unit, :function_name).

scope(:controller) do
  function(:show) do
    @user = User.find(params[:id])
  end

  function(:index) do
    @users = User.active
  end
end

render

The view path app/units/example_unit/views is added into view_paths in invoking. So you can render app/units/example_unit/views/_example.html.slim in invoking.

scope(:view) do
  function(:render_example) do
    render "/example", :foo => hello("world")
  end
end

models

In models block, you can expand model features by expand method. The expanded methods are available via unit proxy like User.unit.active, User.find(params[:id]).unit.active? or User.unit.gc_all_soft_deleted_users.

models do
  expand(:User) do
    scope :active, lambda { where(:deleted_at => nil) }

    def active?
      deleted_at.nil?
    end

    class_methods do
      def gc_all_soft_deleted_users
        where.not(deleted_at: nil).delete_all
      end
    end
  end
end

shared

You can call methods defined by shared in invoking.

shared(:hello) do |world|
  "Hello, #{world}"
end

helpers

You can call helpers in view via unit proxy like unit.helper_method.

helpers do
  def helper_method
    "helper method"
  end
end

Example

https://github.com/cookpad/chanko/tree/master/spec/dummy
Chanko provides an example rails application in spec/dummy directory.

$ git clone [email protected]:cookpad/chanko.git
$ cd chanko/spec/dummy
$ bundle install
$ bundle exec rake db:create db:migrate
$ rails s
$ open http://localhost:3000

chanko's People

Contributors

eudoxa avatar r7kamura avatar eagletmt avatar sorah avatar takai avatar gfx avatar amatsuda avatar makimoto avatar taiki45 avatar tricknotes avatar ma2gedev avatar hotchpotch avatar sachin21 avatar l15n avatar tapster avatar yoshiori avatar daichirata avatar mirakui avatar mrkn avatar katsuma avatar y310 avatar sanemat avatar y-matsuwitter avatar

Watchers

Keon Ye 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.