transform id columns when calling pluck on array

Change-Id: I0c8e8417bcead9d9e1243dabae1444fae3e3634b
Reviewed-on: https://gerrit.instructure.com/150278
Reviewed-by: Mysti Sadler <mysti@instructure.com>
Reviewed-by: Cody Cutrer <cody@instructure.com>
Tested-by: Jenkins
Product-Review: James Williams  <jamesw@instructure.com>
QA-Review: James Williams  <jamesw@instructure.com>
This commit is contained in:
James Williams 2018-05-15 06:55:15 -06:00
parent f78f1f911f
commit e293b2ca65
2 changed files with 22 additions and 1 deletions

View File

@ -105,3 +105,13 @@ module RaiseErrorOnDurationCoercion
end
end
ActiveSupport::Duration.prepend(RaiseErrorOnDurationCoercion)
module Enumerable
def pluck(*keys)
if keys.many?
map { |o| keys.map { |key| o.is_a?(ActiveRecord::Base) ? o.send(key) : o[key] } }
else
map { |o| o.is_a?(ActiveRecord::Base) ? o.send(keys.first) : o[keys.first] }
end
end
end

View File

@ -15,7 +15,7 @@
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
require_relative "../spec_helper"
require_relative "../sharding_spec_helper"
describe 'ActiveSupport::JSON' do
it "encodes hash keys correctly" do
@ -28,3 +28,14 @@ describe ActiveSupport::TimeZone do
expect(ActiveSupport::TimeZone['America/Denver'].to_json).to eq "America/Denver".to_json
end
end
describe "enumerable pluck extension" do
specs_require_sharding
it "should transform ids" do
u = User.create!
@shard1.activate do
expect([u].pluck(:id)).to eq [u.global_id]
end
end
end