Giter Site home page Giter Site logo

garysweaver / otw Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 208 KB

Adds current_controller on ActiveRecord model instance so request, etc. can be used in model/model callbacks.

License: MIT License

Ruby 92.77% JavaScript 3.00% CoffeeScript 0.96% CSS 3.28%

otw's Introduction

Build Status Gem Version

otw

Provides current_controller in each model instance in a way that intends to be safe for concurrent access. Throws separation of concerns "out the window" for specific cases where having access to the controller, request, etc. are needed.

Should work with ActiveRecord, and may work with Mongoid and MongoMapper since we try to include the current_controller method on those if they exist.

Please don't misuse this. Models in Rails should not be concerned with the controller. However, sometimes you might want to have a callback that sets the current_controller.request.remote_ip in a field, etc.

Use at your own risk.

How It Works

Uses an around_filter in base controller, so if you have any before_filter doing something where a model might need current_controller, that should work. If you have any trouble with filter order, you can manually call prepend_around_filter :controller_around, or ditch this and write your own, or just set/unset fields as needed on the models in your controller.

Installation

In Gemfile:

gem 'otw'

Then:

bundle install

Usage

In each model instance, there is a current_controller method to get the controller. To get the request, you'd use current_controller.request.

Testing

Has appraisals/travis build with some concurrency testing. TravisCI tests with Ruby 2.0.0, 1.9.3, ruby-head, jruby-19mode, jruby-head in Rails 3.2 and 4.0 via TravisCI. Rails 3.1.x (3.1.12 at least) or its dependencies have some sqlite locking issue that fails with concurrency testing, so we don't concurrency test in Rails 3.1.x. We don't have tests for Mongoid or MongoMapper, yet, and not using them so consider those experimental. PR's and other feedback welcome.

Contributing

Interested in adding support for another ORM, etc.? Just do a pull request, and if you'd like to join the team to help out, let us know.

License

This gem is released under the MIT license.

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.