Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented support for the SUM aggregate function #14

Merged
merged 2 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

## Not released

## 0.10.1
* Added support for 'or' and 'not' clauses (https://github.com/Beyond-Finance/active_force/pull/13)
* Added support for the SUM aggregate function (https://github.com/Beyond-Finance/active_force/pull/14)
## 0.10.0
* Fix `#where` chaining on `ActiveQuery` (https://github.com/Beyond-Finance/active_force/pull/7)
* Add `#find_by!` which raises `ActiveForce::RecordNotFound` if nothing is found. (https://github.com/Beyond-Finance/active_force/pull/8)
* Fix `#includes` to find, build, and set the association. (https://github.com/Beyond-Finance/active_force/pull/12)

## 0.10.1
* Added support for 'or' and 'not' clauses (https://github.com/Beyond-Finance/active_force/pull/13)

## 0.9.1
* Fix invalid error class (https://github.com/Beyond-Finance/active_force/pull/6)

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,16 @@ It is also possible to eager load associations:
Comment.includes(:post)
```

### Aggregates

Summing the values of a column:
```ruby
Transaction.where(offer_id: 'ABD832024').sum(:amount)
#=> This will query "SELECT SUM(Amount__c)
# FROM Transaction__c
# WHERE offer_id = 'ABD832024'"
```

#### Decorator

You can specify a `self.decorate(records)` method on the class, which will be called once with
Expand Down
5 changes: 5 additions & 0 deletions lib/active_force/active_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ def count
sfdc_client.query(to_s).first.expr0
end

def sum field
super(mappings[field])
sfdc_client.query(to_s).first.expr0
end

def limit limit
super
limit == 1 ? to_a.first : self
Expand Down
5 changes: 5 additions & 0 deletions lib/active_force/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ def count
self
end

def sum field
@query_fields = ["sum(#{field})"]
self
end

protected
def and_conditions
"(#{@conditions.join(') AND (')})" unless @conditions.empty?
Expand Down
2 changes: 1 addition & 1 deletion lib/active_force/sobject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SObject

class << self
extend Forwardable
def_delegators :query, :not, :or, :where, :first, :last, :all, :find, :find_by, :find_by!, :count, :includes, :limit, :order, :select, :none
def_delegators :query, :not, :or, :where, :first, :last, :all, :find, :find_by, :find_by!, :sum, :count, :includes, :limit, :order, :select, :none
def_delegators :mapping, :table, :table_name, :custom_table?, :mappings

private
Expand Down
10 changes: 10 additions & 0 deletions spec/active_force/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,14 @@
expect(query.where("name = 'cool'").count.to_s).to eq "SELECT count(Id) FROM table_name WHERE (name = 'cool')"
end
end

describe ".sum" do
it "should return the query for summing the desired column" do
expect(query.sum(:field1).to_s).to eq 'SELECT sum(field1) FROM table_name'
end

it "should work with a condition" do
expect(query.where("name = 'cool'").sum(:field1).to_s).to eq "SELECT sum(field1) FROM table_name WHERE (name = 'cool')"
end
end
end