Skip to content

Commit

Permalink
Merge pull request #7 from Uaitt/fix/active-record-relation-issue
Browse files Browse the repository at this point in the history
Fix: active record relation issue
  • Loading branch information
Uaitt committed Sep 11, 2023
2 parents 631ceef + 8649657 commit 66f6f25
Show file tree
Hide file tree
Showing 16 changed files with 169 additions and 121 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/specs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/type_checking.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ gemspec
group :development, :test do
gem 'activerecord'
gem 'rake'
gem 'rbs'
gem 'rbs', require: false
gem 'rspec'
gem 'rubocop'
gem 'rubocop-performance'
gem 'rubocop-rspec'
gem 'steep'
gem 'steep', require: false
end
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
aasm_rbs (0.2.0)
aasm_rbs (0.2.1)
aasm (~> 5)

GEM
Expand Down
2 changes: 1 addition & 1 deletion aasm_rbs.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require_relative 'lib/aasm_rbs/version'
Gem::Specification.new do |spec|
spec.name = 'aasm_rbs'
spec.version = AasmRbs::VERSION
spec.summary = 'AASM RBS'
spec.summary = 'RBS signatures for AASM classes'
spec.description = 'Easily generate RBS signatures for all the AASM automatically generated methods and constants of your ruby classes.'
spec.license = 'MIT'

Expand Down
2 changes: 1 addition & 1 deletion exe/aasm_rbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby

unless File.exist?('./Rakefile') || File.exist?('./Gemfile')
abort 'Please run aasm_rbs from the project root.'
abort('Please run aasm_rbs from the project root.')
end

require_relative '../lib/aasm_rbs'
Expand Down
19 changes: 12 additions & 7 deletions lib/aasm_rbs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module AasmRbs
def self.load_constants(klass_name)
load './Rakefile' if File.exist?('./Rakefile')
load('./Rakefile') if File.exist?('./Rakefile')
begin
Rake::Task[:environment].invoke
rescue StandardError
Expand All @@ -17,21 +17,26 @@ def self.load_constants(klass_name)
begin
require file
rescue LoadError
abort 'There was a problem loading the class file'
abort('There was a problem loading the class file.')
end
end

def self.run(klass_name)
def self.run(klass_name, opts = {})
klass = Object.const_get(klass_name)
output = Output.new(klass)

states = klass.aasm.states.map(&:name)
events = klass.aasm.events.map(&:name)

output = Output.new(klass)
output.add_states(states)
output.new_line
create_scopes = klass.aasm.state_machine.config.create_scopes
active_record_model = klass.respond_to?(:aasm_create_scope)
opts[:scopes] = true if create_scopes && active_record_model

output.add_states(states, opts)
output.add_events(events)
output.new_line && output.add_active_record_relation if opts[:scopes]
output.finalize
rescue StandardError
print "aasm_rbs received an invalid class name."
abort('aasm_rbs received an invalid class name.')
end
end
27 changes: 19 additions & 8 deletions lib/aasm_rbs/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

module AasmRbs
class Output
attr_reader :data

def initialize(klass)
@klass = klass
superclass = klass.superclass == Object ? nil : " < #{klass.superclass}"
self.data = "class #{klass}#{superclass}\n"
end

def add_states(states)
def add_states(states, opts = {})
add_state_constants(states)
create_scopes = klass.aasm.state_machine.config.create_scopes
active_record_model = klass.respond_to?(:aasm_create_scope)
add_state_scopes(states) if active_record_model && create_scopes
add_state_scopes(states) if opts[:scopes]
add_predicate_states_methods(states)
end

Expand All @@ -25,6 +25,16 @@ def add_events(events)
end
end

def add_active_record_relation
self.data += <<-RBS
class ActiveRecord_Relation < ::ActiveRecord::Relation
include GeneratedRelationMethods
include _ActiveRecord_Relation[#{klass}, Integer]
include Enumerable[#{klass}]
end
RBS
end

def new_line
self.data += "\n"
end
Expand All @@ -36,20 +46,21 @@ def finalize
private

attr_reader :klass
attr_accessor :data
attr_writer :data

def add_state_constants(states)
states.each { |state| self.data += " STATE_#{state.upcase}: String\n" }
self.data += "\n"
new_line
end

def add_state_scopes(states)
states.each { |state| self.data += " def self.#{state}: () -> ::ActiveRecord_Relation\n" }
self.data += "\n"
states.each { |state| self.data += " def self.#{state}: () -> ActiveRecord_Relation\n" }
new_line
end

def add_predicate_states_methods(states)
states.each { |state| self.data += " def #{state}?: () -> bool\n" }
new_line
end
end
end
2 changes: 1 addition & 1 deletion lib/aasm_rbs/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module AasmRbs
VERSION = '0.2.0'
VERSION = '0.2.1'
end
2 changes: 1 addition & 1 deletion rbs_collection.lock.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
sources:
- type: git
name: ruby/gem_rbs_collection
revision: d114ffa80f35b138aceb7660a224d78afa1b2a63
revision: 248499a924c3cb331d40ca667d40528814043788
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
path: ".gem_rbs_collection"
Expand Down
4 changes: 0 additions & 4 deletions rbs_collection.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,3 @@ sources:
path: .gem_rbs_collection

gems:
- name: rbs
ignore: true
- name: steep
ignore: true
4 changes: 2 additions & 2 deletions sig/aasm_rbs.rbs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module AasmRbs
Rake: untyped # there is yet no Rake official RBS

def self.load_constants: (String) -> void
def self.run: (String) -> String?
def self.load_constants: (String klass_name) -> void
def self.run: (String klass_name, ?::Hash[untyped, untyped] opts) -> String?
end

# There are yet no official RBS signatures for the AASM module that gets
Expand Down
17 changes: 10 additions & 7 deletions sig/aasm_rbs/output.rbs
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
module AasmRbs
class Output
def initialize: (Class) -> String
attr_reader data: String

def add_states: (Array[String]) -> Array[String]
def add_events: (Array[String]) -> Array[String]
def initialize: (Class klass) -> String

def add_states: (Array[String] states, ?::Hash[untyped, untyped] opts) -> String
def add_events: (Array[String] events) -> Array[String]
def add_active_record_relation: () -> String

def new_line: () -> String
def finalize: () -> String

private

attr_reader klass: Class
attr_accessor data: String
attr_writer data: String

def add_state_constants: (Array[String]) -> String
def add_state_scopes: (Array[String]) -> String
def add_predicate_states_methods: (Array[String]) -> Array[String]
def add_state_constants: (Array[String] states) -> String
def add_state_scopes: (Array[String] states) -> String
def add_predicate_states_methods: (Array[String] states) -> String
end
end
Loading

0 comments on commit 66f6f25

Please sign in to comment.