alias_association 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c55602c05ac4f57d4ac0e12280430d9bf8d26f18d54f4bd1a0cb4e7c64c05ce8
4
+ data.tar.gz: 39a19f8e93d3f28bde78ae8a4f6165db2ec28ff5b32cd0a491af1a86d12eed21
5
+ SHA512:
6
+ metadata.gz: df8dbf406445c1729273d0c64233fc2323fe38aa1ffaa65663e13cf9e163af8ea3226ccefcff1a9967528f24c15cf28f78ed17132d8ecca815154ff76a1c11f2
7
+ data.tar.gz: 20b7efaba74f77bf5d643ca604db022820edd562e8c629027fc454b5d6b61a79d84780fab4ee2980b122d68912943f86a7b6adfe5439b6253e10f5844e2c2eac
data/CHANGELOG.md ADDED
@@ -0,0 +1,18 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [1.0.0] - 2025-10-19
9
+
10
+ ### Added
11
+ - Initial extraction of `AliasAssociation` concern from main Rails application
12
+ - `alias_association` class method to create aliases for ActiveRecord associations
13
+ - Automatic creation of both getter and setter methods for aliased associations
14
+ - Error handling for invalid association references
15
+ - `association_aliases` method for debugging and introspection
16
+ - Comprehensive test suite
17
+ - Detailed documentation and usage examples in README
18
+ - Ruby 3.0+ and Rails 6.0+ compatibility
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in alias_relationship.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "rspec", "~> 3.0"
10
+ gem "sqlite3", "~> 1.4"
data/README.md ADDED
@@ -0,0 +1,108 @@
1
+ # AliasAssociation
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/alias_association.svg)](https://badge.fury.io/rb/alias_association)
4
+ [![RSpec](https://github.com/dapi/alias_association/workflows/RSpec/badge.svg)](https://github.com/dapi/alias_association/actions)
5
+
6
+ AliasAssociation is a Ruby on Rails concern that provides a simple way to create aliases for ActiveRecord associations, including both getter and setter methods.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'alias_association'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ ```bash
19
+ bundle install
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ Include the `AliasAssociation` concern in your ActiveRecord model and use the `alias_association` class method to create aliases for existing associations.
25
+
26
+ ```ruby
27
+ class Order < ApplicationRecord
28
+ include AliasAssociation
29
+
30
+ belongs_to :user
31
+ belongs_to :income_payment_system, class_name: 'PaymentSystem'
32
+ belongs_to :outcome_payment_system, class_name: 'PaymentSystem'
33
+
34
+ # Create aliases for associations
35
+ alias_association :owner, :user
36
+ alias_association :payment_system_from, :income_payment_system
37
+ alias_association :payment_system_to, :outcome_payment_system
38
+ end
39
+ ```
40
+
41
+ Now you can use the aliases just like the original associations:
42
+
43
+ ```ruby
44
+ order = Order.first
45
+
46
+ # Getter methods
47
+ order.owner # equivalent to order.user
48
+ order.payment_system_from # equivalent to order.income_payment_system
49
+ order.payment_system_to # equivalent to order.outcome_payment_system
50
+
51
+ # Setter methods
52
+ order.owner = User.find(2) # equivalent to order.user = User.find(2)
53
+ order.payment_system_from = ps # equivalent to order.income_payment_system = ps
54
+ order.payment_system_to = ps # equivalent to order.outcome_payment_system = ps
55
+ ```
56
+
57
+ ### Error Handling
58
+
59
+ The concern validates that the original association exists before creating the alias:
60
+
61
+ ```ruby
62
+ class Order < ApplicationRecord
63
+ include AliasRelationship
64
+
65
+ # This will raise an ArgumentError if :nonexistent_association doesn't exist
66
+ alias_association :alias_name, :nonexistent_association
67
+ # => ArgumentError: Association :nonexistent_association not found
68
+ ```
69
+
70
+ ### Debug Information
71
+
72
+ You can retrieve information about association aliases for debugging purposes:
73
+
74
+ ```ruby
75
+ Order.association_aliases
76
+ # => { owner: :user, payment_system_from: :income_payment_system, payment_system_to: :outcome_payment_system }
77
+ ```
78
+
79
+ ## API Reference
80
+
81
+ ### `alias_association(new_name, original_name)`
82
+
83
+ Creates an alias for an existing ActiveRecord association.
84
+
85
+ - **`new_name`** (Symbol) - The name for the new alias
86
+ - **`original_name`** (Symbol) - The name of the existing association to alias
87
+
88
+ **Raises:** `ArgumentError` if the original association doesn't exist
89
+
90
+ ### `association_aliases`
91
+
92
+ Returns a hash containing information about all association aliases for the model.
93
+
94
+ **Returns:** `Hash` with alias names as keys and original association names as values
95
+
96
+ ## Development
97
+
98
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
99
+
100
+ 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](https://rubygems.org).
101
+
102
+ ## Contributing
103
+
104
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dapi/alias_association.
105
+
106
+ ## License
107
+
108
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AliasAssociation
4
+ VERSION = "1.0.0"
5
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "alias_association/alias_association_version"
4
+ require "active_support/concern"
5
+
6
+ # This module provides functionality to create aliases for ActiveRecord associations
7
+ module AliasAssociation
8
+ extend ActiveSupport::Concern
9
+
10
+ class_methods do
11
+ # Создает алиас для association, включая геттер и сеттер
12
+ #
13
+ # @param new_name [Symbol] новое имя ассоциации
14
+ # @param original_name [Symbol] оригинальное имя ассоциации
15
+ #
16
+ # @example
17
+ # belongs_to :user
18
+ # alias_association :owner, :user
19
+ # # Создаст методы owner и owner=
20
+ #
21
+ def alias_association(new_name, original_name)
22
+ # Проверяем существование оригинальной ассоциации
23
+ unless reflect_on_association(original_name)
24
+ raise ArgumentError, "Association #{original_name.inspect} not found"
25
+ end
26
+
27
+ # Создаем alias для геттера
28
+ alias_method new_name, original_name
29
+
30
+ # Создаем alias для сеттера
31
+ setter_name = "#{new_name}="
32
+ original_setter_name = "#{original_name}="
33
+
34
+ if method_defined?(original_setter_name)
35
+ alias_method setter_name, original_setter_name
36
+ end
37
+
38
+ # Добавляем информацию об алиасе для возможной отладки
39
+ @association_aliases ||= {}
40
+ @association_aliases[new_name] = original_name
41
+ end
42
+
43
+ # Возвращает хэш с информацией об алиасах ассоциаций
44
+ def association_aliases
45
+ @association_aliases || {}
46
+ end
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alias_association
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Danil Pismenny
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: activerecord
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '6.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '6.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: activesupport
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '6.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '6.0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: rspec
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3.0'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '3.0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: sqlite3
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.4'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.4'
68
+ - !ruby/object:Gem::Dependency
69
+ name: rake
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '13.0'
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '13.0'
82
+ description: A Ruby on Rails concern that provides a simple way to create aliases
83
+ for ActiveRecord associations, including both getter and setter methods.
84
+ email:
85
+ - danil@brandymint.ru
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - CHANGELOG.md
91
+ - Gemfile
92
+ - README.md
93
+ - Rakefile
94
+ - lib/alias_association.rb
95
+ - lib/alias_association/alias_association_version.rb
96
+ homepage: https://github.com/dapi/alias_association
97
+ licenses:
98
+ - MIT
99
+ metadata:
100
+ homepage_uri: https://github.com/dapi/alias_association
101
+ source_code_uri: https://github.com/dapi/alias_association
102
+ changelog_uri: https://github.com/dapi/alias_association/blob/main/CHANGELOG.md
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 3.0.0
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubygems_version: 3.7.2
118
+ specification_version: 4
119
+ summary: ActiveRecord concern for creating aliases for associations
120
+ test_files: []