Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
31 changes: 31 additions & 0 deletions lib/generators/active_operator/operation_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'rails/generators'
require 'active_support/inflector'

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

check_class_collision suffix: "Operation"

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/#{operation_file_path}_operation.rb"
end

private

def file_name
@_file_name ||= super.sub(/_operation\z/i, "")
end

def operation_file_path
name.underscore.sub(/_operation\z/i, "")
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 %>Operation < ApplicationOperator
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_operation.rb"

operation_contents = File.read(File.join(destination_root, "app/operations/user_operation.rb"))
assert_match "class UserOperation < ApplicationOperator", 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_operation.rb"

operation_contents = File.read(File.join(destination_root, "app/operations/geocode/v1/pull_operation.rb"))
assert_match "class Geocode::V1::PullOperation < ApplicationOperator", 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_operation.rb"

operation_contents = File.read(File.join(destination_root, "app/operations/api/v2/users/profile/update_operation.rb"))
assert_match "class Api::V2::Users::Profile::UpdateOperation < ApplicationOperator", 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 < ApplicationOperator", 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 < ApplicationOperator", operation_contents
end
end
end
end