Skip to content

Commit

Permalink
bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
emfy0 committed Sep 17, 2024
1 parent fac79fc commit 9f7478a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Parameters:
- `scope`: A lambda defining a scope that fetches the virtual field value (optional).
- `select`: SQL selection logic (can be a `string | arel_node` or a lambda returning an SQL `string | 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.
- `default`: A default value for the virtual field if the result is nil (optional).

Example:

Expand Down Expand Up @@ -60,6 +60,16 @@ User.with_total_orders.where(total_orders: 5)

This will include the total_orders virtual field in the SQL query and allow filtering by it.

### Also it can be used without scope:

```ruby
class User < ApplicationRecord
virtual_field :fullname,
select: -> { "name || surname" },
get: -> { "#{name}#{surname}" }
end
```

## Development

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.
Expand Down
Binary file modified ar_virtual_field.gem
Binary file not shown.
2 changes: 1 addition & 1 deletion lib/ar_virtual_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def self.select_append(relation, *values)
end
end

def virtual_field(name, scope: nil, select:, get:, default:)
def virtual_field(name, scope: nil, select:, get:, default: nil)
name = name.to_s
current_class = self
unwrap_arel_expression = -> (exp) { exp.is_a?(Arel::Nodes::NodeExpression) ? exp : Arel.sql(exp) }
Expand Down
2 changes: 1 addition & 1 deletion lib/ar_virtual_field/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module ArVirtualField
VERSION = "0.3.0"
VERSION = "0.4.0"
end

0 comments on commit 9f7478a

Please sign in to comment.