├── lib ├── transactional │ ├── version.rb │ └── railtie.rb └── transactional.rb ├── .travis.yml ├── .gitignore ├── test ├── test_helper.rb └── transactional_test.rb ├── bin ├── setup └── console ├── Gemfile ├── Rakefile ├── transactional.gemspec ├── LICENSE.txt └── README.md /lib/transactional/version.rb: -------------------------------------------------------------------------------- 1 | module Transactional 2 | VERSION = "0.1.0" 3 | end 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: ruby 3 | rvm: 4 | - 2.4.1 5 | before_install: gem install bundler -v 1.15.3 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /Gemfile.lock 4 | /_yardoc/ 5 | /coverage/ 6 | /doc/ 7 | /pkg/ 8 | /spec/reports/ 9 | /tmp/ 10 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__) 2 | require "transactional" 3 | 4 | require "minitest/autorun" 5 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | set -vx 5 | 6 | bundle install 7 | 8 | # Do any other automated setup that you need to do here 9 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } 4 | 5 | # Specify your gem's dependencies in transactional.gemspec 6 | gemspec 7 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | require "rake/testtask" 3 | 4 | Rake::TestTask.new(:test) do |t| 5 | t.libs << "test" 6 | t.libs << "lib" 7 | t.test_files = FileList["test/**/*_test.rb"] 8 | end 9 | 10 | task :default => :test 11 | -------------------------------------------------------------------------------- /test/transactional_test.rb: -------------------------------------------------------------------------------- 1 | require "test_helper" 2 | 3 | class TransactionalTest < Minitest::Test 4 | def test_that_it_has_a_version_number 5 | refute_nil ::Transactional::VERSION 6 | end 7 | 8 | def test_it_does_something_useful 9 | assert false 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /lib/transactional/railtie.rb: -------------------------------------------------------------------------------- 1 | module Transactional 2 | class Railtie < Rails::Railtie 3 | initializer 'transactional.configure_rails_initialization' do 4 | ActiveSupport.on_load(:action_controller) do 5 | ApplicationController.send(:extend, ::Transactional) 6 | end 7 | end 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require "bundler/setup" 4 | require "transactional" 5 | 6 | # You can add fixtures and/or initialization code here to make experimenting 7 | # with your gem easier. You can also use a different console, if you like. 8 | 9 | # (If you use this, don't forget to add pry to your Gemfile!) 10 | # require "pry" 11 | # Pry.start 12 | 13 | require "irb" 14 | IRB.start(__FILE__) 15 | -------------------------------------------------------------------------------- /lib/transactional.rb: -------------------------------------------------------------------------------- 1 | require "transactional/version" 2 | require "transactional/railtie" 3 | 4 | module Transactional 5 | def transactional(*actions) 6 | _module = Module.new do 7 | actions.each do |action| 8 | define_method action do 9 | ActiveRecord::Base.transaction { super() } 10 | end 11 | end 12 | end 13 | 14 | const_set :Transactional, _module 15 | 16 | prepend _module 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /transactional.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path("../lib", __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require "transactional/version" 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "transactional" 8 | spec.version = Transactional::VERSION 9 | spec.authors = ["Tsukuru Tanimichi"] 10 | spec.email = ["info+git@ttanimichi.com"] 11 | spec.summary = "Declarative transaction management for Ruby on Rails" 12 | spec.homepage = "https://github.com/ttanimichi/transactional" 13 | spec.license = "MIT" 14 | spec.require_paths = ["lib"] 15 | 16 | spec.files = `git ls-files -z`.split("\x0").reject do |f| 17 | f.match(%r{^(test|spec|features)/}) 18 | end 19 | 20 | spec.add_development_dependency "bundler", "~> 1.15" 21 | spec.add_development_dependency "rake", "~> 10.0" 22 | spec.add_development_dependency "minitest", "~> 5.0" 23 | end 24 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Tsukuru Tanimichi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Transactional 2 | 3 | Declarative transaction management for Ruby on Rails. 4 | 5 | ## Installation 6 | 7 | Add this line to your application's Gemfile: 8 | 9 | ```ruby 10 | gem 'transactional' 11 | ``` 12 | 13 | And then execute: 14 | 15 | $ bundle 16 | 17 | Or install it yourself as: 18 | 19 | $ gem install transactional 20 | 21 | ## Usage 22 | 23 | Declare actions which use transaction in your Controller. 24 | 25 | ```diff 26 | class YourController < ApplicationController 27 | + transactional :create, :update 28 | + 29 | def index 30 | end 31 | 32 | def create 33 | Post.create!(name: 'john', age: 42) 34 | Topic.create!(title: 'invalid title') 35 | render plain: :created 36 | end 37 | 38 | def update 39 | end 40 | end 41 | ``` 42 | 43 | ## Development 44 | 45 | After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. 46 | 47 | 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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). 48 | 49 | ## Contributing 50 | 51 | Bug reports and pull requests are welcome on GitHub at https://github.com/ttanimichi/transactional. 52 | 53 | ## License 54 | 55 | The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). 56 | --------------------------------------------------------------------------------