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 >= 3.0.0
  • Rails >= 6.1.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)

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

In version 2 and earlier, Chanko extended Rails' search path to include the views path of the unit. This functionality has been discontinued. To maintain the views path under the unit, you will need to manually create a symbolic link in app/views/units to access it.

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

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

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

chanko's Issues

When using zeitwerk, eager_load fails in production mode

This can be avoided by letting zeitwerk do the work instead of using Chanko :: Loader.
So in my project I added the following code in config/application.rb

config.before_configuration do
  Chanko::Config.eager_load = false
end

Is it possible to add a decision process to determine if zeitwerk is used in the next part, like #57?
https://github.com/cookpad/chanko/blob/master/lib/chanko/railtie.rb#L24
https://github.com/cookpad/chanko/blob/master/lib/chanko/railtie.rb#L30

New features

Hi there

I might be missing something but am wondering what the best practice would be when prototyping a feature that requires "new" routes and controllers. The readme covers scenarios where the feature is implemented as an extension to existing controllers, models and views but I don't see a story for units that include new routes and controllers. I guess what I was expecting to see for this was some kind of :routes scope?

Is this something that you've considered and if so can you provide some guidance?

Thanks

Richie

Support for rail 3.2.x

Hi, this is not an issue, but a simple question whether it works with rails 3.2.x versions?

Rails6(開発環境)において、Viewファイル保存後のリロードで、Unit側のViewファイルの呼び出しが失敗する

発生環境

Rails6 + development + zeitwerk及びclassicのview拡張

再現方法

scope(:view) do
  function(:hello) do
    render '/hello'
  end
end

上記をviewファイルから呼び出した状態で、viewファイル「のみ」を保存しリロードすると、下記のエラーが発生する。

  [Chanko] ActionView::Template::Error - undefined method `_app_units_hello_world_views__hello_html_erb__3197552978688360732_19600' for #<ActionView::Base:0x00000000009ad8>

原因(調査中)

Rails6ではViewファイルを保存するたびにActionView::Baseを作り直すようで、Unitで事後に定義したviewファイルの追加設定が反映されない。

Rails6

1. Viewクラスのインスタンス(viewでのself.__id__)
	- リロード => 不変
	- Viewファイル書き換え => 変更
	- Modelファイル書き換え => 変更
2. Model/Unit
	- リロード => 不変
	- Viewファイル書き換え => 不変
	- Modelファイル書き換え => 変更

Rails5

1. Viewクラスのインスタンス(viewでのself.__id__)
	- リロード => 不変
	- Viewファイル書き換え => 不変
	- Modelファイル書き換え => 変更
3. Model/Unit
	- リロード => 不変
	- Viewファイル書き換え => 不変
	- Modelファイル書き換え => 変更

Rails 6.0 x zeitwerk の環境において units_directory_path が固定されてしまう場合がある

背景

cookpad_all では Rails 6.0 で zeitwerk を使っている状況で Chanko を使っています。
また Chanko::Config の設定については config/initializers 以下に Chanko 用のファイルを用意してそこで各種値を設定しており、そこで unit を置くディレクトリを Chanko::Config.units_directory_path = "app/extensions" で変更しています。

問題

上で設定した units_directory_path が反映されず、デフォルトである app/units で固定されてしまいます。

原因

Chanko::Config は一度 units_directory_path を読み出そうとしたタイミングで現在の @units_directory_path の値をキャッシュしています。

def units_directory_path
@resolved_units_directory_path ||= Rails.root.join(@units_directory_path).to_s
end

また、zeitwerk 環境下では以下のコードにより before: :set_autoload_paths のタイミングで Chanko::Loader.prepare_eager_load が呼び出されます。これは initializers 以下のコードが実行されるよりも前のタイミングです。

initializer("chanko.zeitwerk.prepare_eager_load", before: :set_autoload_paths) do |app|
# zeitwerk freezes autoload_paths after :set_autoload_paths.
# So we need to prepare before set_autoload_paths
Chanko::Loader.prepare_eager_load(mode: :zeitwerk)
end

この prepare_eager_load の中で Chanko::Config::units_directory_path が読み出されているんですが、その時点ではまだ units_directory_path がデフォルト値なのでそちらの値でパスが固定されます。

Rails.autoloaders.main.collapse(Chanko::Config.units_directory_path + '/*')

確認したいこと

  • この仕様は意図したしようでしょうか。
  • もし意図した挙動であれば少しハマりやすいポイントなので、以下のような対応をしたいです。
    • ドキュメントにパスの設定は initializers ではなく application.rb や environment などに書くことを記述する。
    • @resolved_units_directory_path に一度値が入ってしまった場合、 @units_directory_path にいくら値を入れても Chanko で利用するパスは変更できないので、units_directory_path=(path)@resolved_units_directory_path が nil でないときに例外を吐くなどをしたい。
  • もし意図していない挙動であればちょっと修正方針を考えたいです。

Suggested way to test against Chanko 2.0?

Recently I started to play and adopt Chanko into our project. It works nicely and really changes the way we work.

However, I found that in Chanko 2.0, the unit generator no longer creates spec files for units, and since there's no need to run install generator in 2.0, the app/units folder is also not symlinked to /spec.

What is the suggested way to test against Chanko 2.0?

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.