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

Adding order method to active query to support several argument formats #58

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Not released

## 0.14.1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove line 5, just leave it as "Not released". We'll package a few changes together before we release.

- Add `#order` method to active query that accepts arguments in several formats ( symbol, string that has raw soql) (https://github.com/Beyond-Finance/active_force/pull/58)
- By default, the order is ascending. For descending order, you can map the column name symbol to :desc
- use-cases: User.order(:name), User.order(email: :desc), User.order(:name, email: :desc), User.order('name')

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove line 7&8. Just need the info on line 6.

Feel free to add info to README.md if you feel additional documentation is necessary, but since this pretty much follows the AR implementation, probably not necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, Would update them

## 0.14.0

- Add `scoped_as` option to `has_one` association (https://github.com/Beyond-Finance/active_force/pull/50)
Expand Down
23 changes: 23 additions & 0 deletions lib/active_force/active_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ def loaded?
!@records.nil?
end

def order *args
return self if args.nil?
super build_order_by args
end

private

def build_condition(args, other=[])
Expand Down Expand Up @@ -209,5 +214,23 @@ def clone_self_and_clear_cache
new_query.instance_variable_set(:@records, nil)
new_query
end

def build_order_by(args)
args.map do |arg|
case arg
when Symbol
mappings[arg].to_s
when Hash
arg.map { |key, value| "#{mappings[key]} #{order_type(value)}" }
else
arg
end
end.join(', ')
end

def order_type(type)
type == :desc ? 'DESC' : 'ASC'
end

end
end
21 changes: 21 additions & 0 deletions spec/active_force/active_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,25 @@
it { is_expected.to be_truthy }
end
end

describe "#order" do
context 'when it is symbol' do
it "should add an order condition with actual SF field name" do
expect(active_query.order(:field).to_s).to eq "SELECT Id FROM table_name ORDER BY Field__c"
end
end

context 'when it is string - raw soql' do
it "should add an order condition same as the string provided" do
expect(active_query.order('Field__c').to_s).to eq "SELECT Id FROM table_name ORDER BY Field__c"
end
end

context 'when it is multiple columns' do
it "should add an order condition with actual SF field name and the provided order type" do
expect(active_query.order(:other_field, field: :desc).to_s).to eq "SELECT Id FROM table_name ORDER BY Other_Field, Field__c DESC"
end
end

end
end