All Projects → antulik → Active_interaction Extras

antulik / Active_interaction Extras

Licence: mit
Useful extensions for active_interaction gem

Programming Languages

ruby
36898 projects - #4 most used programming language

Labels

Projects that are alternatives of or similar to Active interaction Extras

Chrome Extension Cli
🚀 The CLI for your next Chrome Extension
Stars: ✭ 536 (+2877.78%)
Mutual labels:  extension
Hypopg
Hypothetical Indexes for PostgreSQL
Stars: ✭ 594 (+3200%)
Mutual labels:  extension
Group by.ext.ee addon
An ExpressionEngine 2.x extension to add 'group by' support to the channel:entries tag pair.
Stars: ✭ 6 (-66.67%)
Mutual labels:  extension
Minerblock
An efficient browser extension to block browser-based cryptocurrency miners all over the web.
Stars: ✭ 563 (+3027.78%)
Mutual labels:  extension
Flask Caching
A caching extension for Flask
Stars: ✭ 582 (+3133.33%)
Mutual labels:  extension
Vscode Todo Plus
Manage todo lists with ease. Powerful, easy to use and customizable.
Stars: ✭ 622 (+3355.56%)
Mutual labels:  extension
Xcodeway
⛵️ An Xcode Source Editor Extension that helps navigating to many places easier
Stars: ✭ 530 (+2844.44%)
Mutual labels:  extension
Vs Freemarker
FreeMarker language colorization extension for Visual Studio Code
Stars: ✭ 17 (-5.56%)
Mutual labels:  extension
Php Memcache Dll
PHP-memcache-dll
Stars: ✭ 588 (+3166.67%)
Mutual labels:  extension
Javascript
a chrome extension,game for javascript
Stars: ✭ 5 (-72.22%)
Mutual labels:  extension
Safari2aria
safari extension for use aria2 to replace safari default download
Stars: ✭ 563 (+3027.78%)
Mutual labels:  extension
Tonyenc
高性能、跨平台的 PHP7 代码加密扩展 (A high performance and cross-platform encrypt extension for PHP source code)
Stars: ✭ 569 (+3061.11%)
Mutual labels:  extension
Weibo Picture Store
🖼 新浪微博图床 Chrome/Firefox 扩展,支持同步到微相册
Stars: ✭ 624 (+3366.67%)
Mutual labels:  extension
Bettertweetdeck
A browser extension to improve TweetDeck with a lot of features
Stars: ✭ 558 (+3000%)
Mutual labels:  extension
Gata
Bookmarks made better
Stars: ✭ 17 (-5.56%)
Mutual labels:  extension
Pgaudit
PostgreSQL Audit Extension
Stars: ✭ 532 (+2855.56%)
Mutual labels:  extension
Nighttab
A neutral new tab page accented with a chosen colour. Customise the layout, style, background and bookmarks in nightTab.
Stars: ✭ 598 (+3222.22%)
Mutual labels:  extension
Blockly Gamepad
A Blockly extension designed to develop games (made with love ❤)
Stars: ✭ 18 (+0%)
Mutual labels:  extension
Rung Cli
Command line tool for Rung
Stars: ✭ 17 (-5.56%)
Mutual labels:  extension
Weakauras2
World of Warcraft addon that provides a powerful framework to display customizable graphics on your screen.
Stars: ✭ 731 (+3961.11%)
Mutual labels:  extension

ActiveInteraction::Extras

This gem contains the collection of useful extensions to active_interaction gem.

Installation

gem 'active_interaction-extras'

Usage

All

class ApplicationInteraction < ActiveInteraction::Base
  include ActiveInteraction::Extras::All
  # same as
  # include ActiveInteraction::Extras::Halt
  # include ActiveInteraction::Extras::ModelFields
  # include ActiveInteraction::Extras::RunCallback
  # include ActiveInteraction::Extras::StrongParams
  # include ActiveInteraction::Extras::Transaction
end

Halt

class Service < ActiveInteraction::Base
  include ActiveInteraction::Extras::Halt

  def execute
    other_method
    puts('finished') # this won't be called
  end

  def other_method
    errors.add :base, :invalid
    halt! if errors.any?
    # or
    halt_if_errors!
  end
end

ModelFields

class UserForm < ActiveInteraction::Base
  include ActiveInteraction::Extras::ModelFields

  interface :user

  model_fields(:user) do
    string :first_name
    string :last_name
  end

  def execute
    model_fields(:user)                   # => {:first_name=>"Albert", :last_name=>"Balk"}
    any_changed?(:first_name, :last_name) # => true
    given_model_fields(:user)             # => {:first_name=>"Albert"}
    changed_model_fields(:user)           # => {:first_name=>"Albert"}
  end
end

user = OpenStruct.new(first_name: 'Sam', last_name: 'Balk')

UserForm.new(user: user).first_name # => 'Sam'
UserForm.run!(user: user, first_name: 'Albert')

RunCallback

class Service < ActiveInteraction::Base
  include ActiveInteraction::Extras::RunCallback

  after_run do
    # LogAttempt.log
  end

  after_successful_run do
    # Email.deliver
  end

  after_failed_run do
    # NotifyAdminEmail.deliver
  end

  def execute
  end
end

StrongParams

class UpdateUserForm < ActiveInteraction::Base
  include ActiveInteraction::Extras::StrongParams

  string :first_name, default: nil, permit: true
  string :last_name, default: nil

  def execute
    first_name # => 'Allowed'
    last_name  # => nil
  end
end

UpdateUserForm.new.to_model.model_name.param_key # => 'update_user_form'

form_params = ActionController::Parameters.new(
  update_user_form: {
    first_name: 'Allowed',
    last_name: 'Not allowed',
  },
)

Service.run(params: form_params)

# OR
form_params = ActionController::Parameters.new(
  first_name: 'Allowed',
  last_name: 'Not allowed',
)

Service.run(form_params: form_params)

Transaction

class UpdateUserForm < ActiveInteraction::Base
  include ActiveInteraction::Extras::Transaction

  run_in_transaction!

  def execute
    Comment.create! # succeeds

    errors.add(:base, :invalid)
  end
end

UpdateUserForm.run
Comment.count # => 0

ActiveJob

class ApplicationInteraction < ActiveInteraction::Base
  include ActiveInteraction::Extras::ActiveJob

  class Job < ActiveJob::Base
    include ActiveInteraction::Extras::ActiveJob::Perform
  end
end

class DoubleService < ApplicationInteraction
  integer :x

  def execute
    x + x
  end
end

DoubleService.delay.run(x: 2) # queues to run in background

Sidekiq

class ApplicationInteraction < ActiveInteraction::Base
  include ActiveInteraction::Extras::Sidekiq

  class Job
    include Sidekiq::Worker
    include ActiveInteraction::Extras::Sidekiq::Perform
  end
end

class DoubleService < ApplicationInteraction
  job do
    sidekiq_options retry: 1
  end

  integer :x

  def execute
    x + x
  end
end

DoubleService.delay.run(x: 2) # queues to run in background

Rspec

class SomeService < ActiveInteraction::Base
  integer :x
end

RSpec.describe SomeService do
  include ActiveInteraction::Extras::Rspec

  it 'works' do
    expect_to_execute(SomeService,
      with: [{ x: 1 }]
      return: :asd
    )

    result = SomeService.run! x: 1

    expect(result).to eq :asd
  end

  it 'lists all mocks' do
    # allow_to_run
    # allow_to_execute
    # allow_to_delay_run
    # allow_to_delay_execute

    # expect_to_run / expect_not_to_run / expect_to_not_run
    # expect_to_execute
    # expect_to_delay_run / expect_to_not_run_delayed
    # expect_to_delay_execute
  end
end

Development

After checking out the repo, run bin/setup to install dependencies. You can also 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, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/antulik/active_interaction-extras. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Credits

ActiveInteraction::Extras is brought to you by Anton Katunin and was originally built at CarNextDoor.

Code of Conduct

Everyone interacting in the ActiveInteraction::Extras project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].