Giter Site home page Giter Site logo

tracyloisel / transactify Goto Github PK

View Code? Open in Web Editor NEW

This project forked from igorkasyanchuk/transactify

1.0 1.0 0.0 79 KB

Wrap your methods in DB Transactions

Home Page: https://www.railsjazz.com/

License: MIT License

Ruby 98.85% Shell 1.15%

transactify's Introduction

Transactify

Code Climate Build Status Gem Version RailsJazz https://www.patreon.com/igorkasyanchuk

Sample

Transactify gem can run your methods in database transaction. Previously you had to wrap you code in ActiveRecord::Base.transaction do .. end but now it can be done in much more simpler way.

Sample or usage: https://github.com/igorkasyanchuk/transactify/blob/master/spec/app.rb#L20

Installation

Add this line to your application's Gemfile:

gem 'transactify'

And then execute:

$ bundle

Or install it yourself as:

$ gem install transactify

Note: works only for Ruby 2+

Usage

Let's say you have model Question with method reject!.

  def reject!(comment_text = nil)
    assignment_log = question_logs.where(action_type: ASSIGNED_STATUS).last
    owner.rejected_questions << self
    owner.update_attribute(average_assigned_questions_count: question_count/total_questions)
    log = question_logs.create(assignee: assignment_log.assigner, assigner: assignment_log.assignee, action_type: REJECTED_STATUS)
    QuestionMailer.update(self, log).deliver_later
  end

Now imagine that total_questions returns 0. And you will have an exception (dividing by zero), but one operation (owner.rejected_questions << self) will be performed to the DB. This is wrong, and normally you can avoid this by adding Transactions:

  def reject!(comment_text = nil)
    ActiveRecord::Base.transaction do
      assignment_log = question_logs.where(action_type: ASSIGNED_STATUS).last
      owner.rejected_questions << self
      owner.update_attribute(average_assigned_questions_count: question_count/total_questions)
      log = question_logs.create(assignee: assignment_log.assigner, assigner: assignment_log.assignee, action_type: REJECTED_STATUS)
      QuestionMailer.update(self, log).deliver_later
    end
  end

But what if you have many-many such methods? This is where transactify gem can help. You can speficy which methods you want to run in transaction. So final code will looks like:

  def reject!(comment_text = nil)
    assignment_log = question_logs.where(action_type: ASSIGNED_STATUS).last
    owner.rejected_questions << self
    owner.update_attribute(average_assigned_questions_count: question_count/total_questions)
    log = question_logs.create(assignee: assignment_log.assigner, assigner: assignment_log.assignee, action_type: REJECTED_STATUS)
    QuestionMailer.update(self, log).deliver_later
  end

  transactify :reject!  # With this line you can specify which methods you want to make safe for DB

Main benefit of this gem is that you don't need to edit all your methods and add transaction blocks. So without any existing code modification you can add support for transaction for whole methods.

Gem allows to transactify instance and class methods.

Functionality

include Transactify - put in your classes, models to add support for transactions

transactify :method_name - transactify your insatance method.

ctransactify :method_name - transactify your class method.

Samples of Usage

class Question < ActiveRecord::Base
  include Transactify

  transactify :reject!, :save_question
  ctransactify :generate_report

  def reject!(comment_text = nil)
    assignment_log = question_logs.where(action_type: ASSIGNED_STATUS).last
    owner.rejected_questions << self
    owner.update_attribute(average_assigned_questions_count: question_count/total_questions)
    log = question_logs.create(assignee: assignment_log.assigner, assigner: assignment_log.assignee, action_type: REJECTED_STATUS)
    QuestionMailer.update(self, log).deliver_later
  end

  def save_question(user, answer, comment_content = nil)
    update_attributes(owner_id: user.id)
    question_logs.create(assignee: user, assigner: user, action_type: SAVE_STATUS, answers: answer)
    save_comment(user, comment_content)
  end

  def self.generate_report
    report = Report.create_new_report
    report.create_schema
    report.populate_users
    report.populate_questions
    report
  end

end

Plans

  • Add support for Sequel gem
  • Add more specs

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release to create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

  1. Fork it ( https://github.com/[my-github-username]/transactify/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

transactify's People

Contributors

igorkasyanchuk avatar

Watchers

 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.