Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/emfy0/ar_virtual_field
Provide an easy mechanism to define virtual fields within an ActiveRecord model
https://github.com/emfy0/ar_virtual_field
activerecord rails ruby sql
Last synced: about 1 month ago
JSON representation
Provide an easy mechanism to define virtual fields within an ActiveRecord model
- Host: GitHub
- URL: https://github.com/emfy0/ar_virtual_field
- Owner: emfy0
- License: mit
- Created: 2024-09-17T12:55:56.000Z (2 months ago)
- Default Branch: main
- Last Pushed: 2024-09-19T17:43:51.000Z (2 months ago)
- Last Synced: 2024-10-06T10:20:18.132Z (about 1 month ago)
- Topics: activerecord, rails, ruby, sql
- Language: Ruby
- Homepage:
- Size: 34.2 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Active Record Virtual Field
Gem provides an easy mechanism to define virtual fields within an ActiveRecord model.
It allows defining attributes that are not directly stored in the database, but are instead computed or fetched using SQL queries.
Gem handles both the definition of these fields and the creation of scopes to query models with the virtual field included.## Installation
Add gem to Gemfile:
```ruby
gem 'ar_virtual_field'
```## Usage
### Defining a virtual field
To define a virtual field, use the virtual_field method in your model:
```ruby
virtual_field :virtual_attribute,
scope: -> { joins(:related_model).where(related_models: { some_column: value }) },
select: -> { "SUM(related_models.some_value)" },
get: -> { calculate_some_value },
default: 0
```Parameters:
- `name`: The name of the virtual field.
- `scope`: A lambda defining a scope that fetches the virtual field value (optional).
- `select`: SQL selection logic (can be a string or arel node or a lambda returning a string or arel_node) to define how the field is computed.
- `get`: A method to retrieve the value of the virtual field when the field isn't fetched via SQL.
- `default`: A default value for the virtual field if the result is nil (optional).Example:
```ruby
class User < ApplicationRecord
virtual_field :total_orders,
scope: -> { joins(:orders).group(:id) },
select: "COUNT(orders.id)",
get: -> { orders.count },
default: 0virtual_field :fullname,
select: "name || surname",
get: -> { "#{name}#{surname}" }
endusers_with_orders = User.with_total_orders # queries database once
user = users_with_orders.first
user.total_orders # => value computed by database
user.fullname # => value computed by ruby
```### Scopes and querying:
- `with_#{name}`: Automatically generated scope to include the virtual field in queries. You can use this scope in your ActiveRecord queries like so:
```ruby
User.with_total_orders.where(User.virtual_fields[:total_orders].eq(5))
```This will include the total_orders virtual field in the SQL query and allow filtering by it.
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/emfy0/ar_virtual_field. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/emfy0/ar_virtual_field/blob/main/CODE_OF_CONDUCT.md).
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
## Code of Conduct
Everyone interacting in the ArVirtualField project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/emfy0/ar_virtual_field/blob/main/CODE_OF_CONDUCT.md).