Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ rails db:migrate

### 1. Define an Operation

Create operation classes that inherit from `ApplicationOperation`:
Generate an operation class by running `rails generate active_operator:operation [name]`.

```ruby
class Geocoding::V1 < ApplicationOperation
Expand Down
20 changes: 20 additions & 0 deletions lib/generators/active_operator/operation_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'rails/generators'
require 'active_support/inflector'

module ActiveOperator
module Generators
class OperationGenerator < Rails::Generators::NamedBase
include Rails::Generators::ResourceHelpers

check_class_collision

source_root File.expand_path("templates", __dir__)

desc "Generates an operation with the given NAME."

def create_operation_file
template "operation.rb.erb", "app/operations/#{file_path}.rb"
end
end
end
end
22 changes: 22 additions & 0 deletions lib/generators/active_operator/templates/operation.rb.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class <%= class_name %> < ApplicationOperation
def request
# faraday.get(
# "https://api.geocod.io/v1.8/geocode",
# {
# q: record.address,
# api_key: Rails.application.credentials.dig(:geocodio, :api_key),
# fields: "timezone"
# }
# )
end

def process
# result = response.dig("body", "results", 0)

# record.update!(
# latitude: result.dig("location", "lat"),
# longitude: result.dig("location", "lng"),
# timezone: result.dig("fields", "timezone", "name")
# )
end
end
69 changes: 69 additions & 0 deletions test/generators/active_operator/operation_generator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# frozen_string_literal: true

require "test_helper"

require "rails/generators"
require "generators/active_operator/operation_generator"

module ActiveOperator
module Generators
class OperationGeneratorTest < Rails::Generators::TestCase
tests ActiveOperator::Generators::OperationGenerator

destination File.expand_path("../../tmp", __dir__)
setup :prepare_destination

def after_teardown
FileUtils.rm_rf destination_root
super
end

test "should generate operation file with simple name" do
run_generator ["user"]

assert_file "app/operations/user.rb"

operation_contents = File.read(File.join(destination_root, "app/operations/user.rb"))
assert_match "class User < ApplicationOperation", operation_contents
assert_match "def request", operation_contents
assert_match "def process", operation_contents
end

test "should generate operation file with nested directories" do
run_generator ["geocode/v1/pull"]

assert_file "app/operations/geocode/v1/pull.rb"

operation_contents = File.read(File.join(destination_root, "app/operations/geocode/v1/pull.rb"))
assert_match "class Geocode::V1::Pull < ApplicationOperation", operation_contents
end

test "should generate operation file with deeply nested directories" do
run_generator ["api/v2/users/profile/update"]

assert_file "app/operations/api/v2/users/profile/update.rb"

operation_contents = File.read(File.join(destination_root, "app/operations/api/v2/users/profile/update.rb"))
assert_match "class Api::V2::Users::Profile::Update < ApplicationOperation", operation_contents
end

test "should handle operation suffix correctly" do
run_generator ["user_operation"]

assert_file "app/operations/user_operation.rb"

operation_contents = File.read(File.join(destination_root, "app/operations/user_operation.rb"))
assert_match "class UserOperation < ApplicationOperation", operation_contents
end

test "should handle nested operation with operation suffix" do
run_generator ["geocode/v1/pull_operation"]

assert_file "app/operations/geocode/v1/pull_operation.rb"

operation_contents = File.read(File.join(destination_root, "app/operations/geocode/v1/pull_operation.rb"))
assert_match "class Geocode::V1::PullOperation < ApplicationOperation", operation_contents
end
end
end
end