https://rainveiltech.com/posts/rails-writing-engine-tests-that-depend-on-main-application-models
https://netguru.co/blog/rails-api-as-an-engine another approach??
| module GitHub | |
| module CSP | |
| # Public: Constants for CSP keywords | |
| NONE = "'none'".freeze | |
| SELF = "'self'".freeze | |
| UNSAFE_INLINE = "'unsafe-inline'".freeze | |
| UNSAFE_EVAL = "'unsafe-eval'".freeze | |
| # Public: Constants for CSP directive names | |
| BASE_URI = "base-uri".freeze |
| config.middleware.insert_before 0, "Rack::Cors", debug: true, logger: ->{Rails.logger} do | |
| allow do | |
| origins 'localhost:3000' | |
| resource %r{/users\/?\d*}, | |
| :headers => ['Origin', 'Accept', 'Content-Type','Token'], | |
| :methods => [:get, :put, :delete], | |
| :max_age => 0 | |
| end | |
| end |
| import { Component } from "React"; | |
| export var Enhance = ComposedComponent => class extends Component { | |
| constructor() { | |
| this.state = { data: null }; | |
| } | |
| componentDidMount() { | |
| this.setState({ data: 'Hello' }); | |
| } | |
| render() { |
| class Importer | |
| def parse_csv_file(csv_file) | |
| File.open(csv_file) do |file| | |
| ActiveRecordModel.transaction do | |
| import_csv_stream(file) | |
| end | |
| end | |
| end | |
| end |
| ActiveRecord::Base.transaction do | |
| @order.destroy! | |
| @user.save! | |
| end | |
| # calling the bang methods will raise an exception and cause the transaction not to occur if errors exist. calling save and non-bang will not raise | |
| # any errors at all | |
| * Each transaction opens up a new database connection |
As your business logic gets complex you may need to implement transactions. The classic example is a bank funds transfer from account A to account B. If the withdrawal from account A fails then the deposit to account B should either never take place or be rolled back.
All the complexity is handled by ActiveRecord::Transactions. Any model class or instance has a method named .transaction. When called and passed a block, that block will be executed inside a database transaction. If there's an exception raised, the transaction will automatically be rolled back.
| begin | |
| update_purchase_captured_at!(purchase, captured_at) | |
| rescue Exception => e | |
| Honeybadger.notify(e) | |
| Rails.logger.error(%Q{\ | |
| [#{Time.zone.now}][rake purchases:update_captured_at] Error updating \ | |
| captured_at for purchase #{purchase.id}: #{e.class}: #{e.message} | |
| This means the funds got captured, but the order will *not* be fulfilled! |
| daemon off; | |
| # Heroku dynos have at least 4 cores. | |
| worker_processes <%= ENV['NGINX_WORKERS'] || 4 %>; | |
| events { | |
| use epoll; | |
| accept_mutex on; | |
| worker_connections 1024; | |
| } |
| class User | |
| attr_accessor :name, :pet_name | |
| end | |
| class Post | |
| end | |
| class Factory < BasicObject | |
| def initialize | |
| @attributes = {} |