• Stars
    star
    55
  • Rank 537,381 (Top 11 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Wrap your methods in DB Transactions

Transactify

Code Climate Build Status Gem Version RailsJazz https://www.patreon.com/igorkasyanchuk Listed on OpenSource-Heroes.com

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

More Repositories

1

rails_db

Rails Database Viewer and SQL Query Runner
JavaScript
1,447
star
2

active_storage_validations

Do it like => validates :photos, attached: true, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: { less_than: 500.kilobytes }, limit: { min: 1, max: 3 }, aspect_ratio: :landscape, dimension: { width: { in: 800..1600 }
Ruby
959
star
3

rails_performance

Monitor performance of you Rails applications (self-hosted and free)
Ruby
793
star
4

any_login

Easy way to login as any user in system
Ruby
376
star
5

log_analyzer

Rails logs analyzer (see how fast your views are rendering)
Ruby
349
star
6

rails_pdf

A reliable way to generate PDF of any complexity in Ruby on Rails apps
HTML
175
star
7

fake_api

The fastest way to prototype API in your Rails application
Ruby
142
star
8

execution_time

How fast is your code? See it directly in Rails console.
Ruby
111
star
9

benchmark_methods

Benchmark and measure execution time your Ruby methods without an additional code changes
Ruby
89
star
10

new_google_recaptcha

reCAPTCHA v3 Ruby on Rails gem
Ruby
82
star
11

sql_view

Rails SQL Views made easy ;)
Ruby
49
star
12

sweet_staging

Access your Rails console, see logs, execute rake commands directly from the browser. Great addition to your Staging ENV.
JavaScript
46
star
13

execute_sql

Execute SQL inside Rails console, or app itself
Ruby
41
star
14

cache_with_locale

Easy wait to do view caching with automatically added "locale" value to the cached key.
Ruby
37
star
15

avatarro

Generate google-style avatars in your application
Ruby
37
star
16

awesome_back_url

Redirect the user to the proper "back" page
Ruby
33
star
17

records_count

See in development logs how many records your queries returns. It can help with solving performance issues.
Ruby
31
star
18

new_ckeditor

Ruby on Rails + CKEditor 5
Ruby
31
star
19

amazon_static_site

Static website using https with your own domain name using Amazon S3 and Cloudflare for FREE
Ruby
29
star
20

omg_image

Generate PNG previews for HTML snippets (html/css/js). Any complexity.
Ruby
28
star
21

wrapped_print

Easy print debug information to your console in Ruby/Rails app.
Ruby
23
star
22

calculate_in_group

Group Active Record by ranges or set of values with a single SQL query.
Ruby
22
star
23

embed_view

Embed ERB files inside another ERB files for faster performance (5-20% BOOST!!!)
HTML
21
star
24

rails_time_travel

HTML
19
star
25

sabotage

Coding & debugging must be fun. Make life a bit harder for your colleagues :)
Ruby
18
star
26

mechanical

All models in a single table, new attributes without migrations. Works like regular AR model
Ruby
17
star
27

secrett11tto

Simple way to protect your content from copy-pasting
Ruby
15
star
28

rails_live

Ruby
15
star
29

mini-guard

Ruby
14
star
30

railsjazz.com

Rails Jazz (personal web site)
JavaScript
14
star
31

sidekiq_log_analyzer

SidekiqLogAnalyser gem allows to see summary of your sidekiq workers (based on log file).
Ruby
13
star
32

hasharay_ext

Painless work with complex Ruby hashes/arrays.
Ruby
13
star
33

active_storage_silent_logs

The idea of this gem is to hide as much as possible Active Storage logs from console so you can see only important information and requests
Ruby
13
star
34

rails_cached_method

Simple way to cache results of methods.
Ruby
11
star
35

with_record

Returns relations/association for soft deleted records in DB
Ruby
10
star
36

rrr

Run recent rspec files only (the only recently modified).
Ruby
9
star
37

font_awesome_file_icons

Ruby
4
star
38

unwhere

Ruby
4
star
39

travel_and_talk

JavaScript
3
star
40

lazy_mobile_tester

Rails Lazy Mobile Tester
Ruby
3
star
41

serpjazz

SERP keywords tracking
JavaScript
3
star
42

jeanappv2

JavaScript
2
star
43

slim_erb_backport

Slim 4+ and ERB friends again :)
Ruby
2
star
44

mega-simple-authorization

mega simple authorization plugin for RoR
Ruby
2
star
45

tv

eb5 tv
JavaScript
2
star
46

spring_rspec_commands_addon

rails+spring+rspec = friends :)
Ruby
2
star
47

layouts_from_db_sample

Allow store layouts to DB (Sample)
2
star
48

CheaperDrinker

CheaperDrinker web site
JavaScript
2
star
49

tell_my_env

Ruby
2
star
50

any_login_test

AnyLogin gem test application
Ruby
2
star
51

VerySimple

1
star
52

ShareT

online translations
JavaScript
1
star
53

test-ec2

test-ec2
1
star
54

sa1

1
star
55

better_tempfile

Ruby
1
star
56

ar_enumerations_test_application

ActiveRecord enumeration field type - test application
Ruby
1
star
57

tophouse.com.ua

JavaScript
1
star
58

capistranotest

1
star
59

zip_and_phone

Zips & Phones
Ruby
1
star
60

portfolio

1
star
61

tdemo

tdemo
Ruby
1
star
62

cisarska_and_frankivska

Cisarska & Frankivska
JavaScript
1
star
63

seminars

JavaScript
1
star
64

deprecations_collector

Save all Rails deprecation in log file for future investigation
Ruby
1
star
65

just_for_fun

Do you want to call 42.to_user, [42, 43, 44].to_users? Try this gem :)
Ruby
1
star
66

rails_logs

Ruby
1
star