grape_ape_rails 0.9.10 → 0.9.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b1240d678239c0a950d300e1156f6016e37c87ad
4
- data.tar.gz: 883a54b7f9f0898db02a7143a4c032a72470e08d
3
+ metadata.gz: 5f2fcc70c094393af40e932cf984884bb1aa8cb8
4
+ data.tar.gz: 837f65908d60bd2924f23f51532cbf3b7ea371da
5
5
  SHA512:
6
- metadata.gz: 5861891416f4090aaccc2eb37d4862d7f62ed5042eecdcb8ac4147be31ed93901bb0ad333152ff1790a8063ab9722c975c7b761fe33831fc74665a4336e002b3
7
- data.tar.gz: 633a04ae35c65fdc5d00612444bc5d82c691acaae03971bce2afdd3a9ae0975c9bbaf3e4ad3bbda6ca601609cbfdf04b40789507e5339ff5c4c153c2f9a55641
6
+ metadata.gz: 2a93445e2f8ce21164b16b55db1a900c2ccabe335a8f96f8cbaba04275355b7f284d315017d5be7ff6884c7807c9b3c4174e189e13f342f5a2a313c2203cccd3
7
+ data.tar.gz: e8aa4a94221ff6b4f3030f9f726ffca2f46e17d66d5e50b44616e28ad7ae36d64c419cc05cf601ab26da3eafc629d90a38aad4f10fd29d88e3c3282c7d8f3233
@@ -34,26 +34,34 @@ module Grape
34
34
  module GarActiveModelSerializers
35
35
  def self.call(resource, env)
36
36
  if serializer = Grape::Formatter::ActiveModelSerializers.fetch_serializer(resource, env)
37
- single = serializer.try(:resource_singular) || serializer.instance_variable_get(:@resource_name).try(:singularize) || serializer.instance_variable_get(:@object).class.name.underscore
38
- plural = serializer.try(:resource_plural) || serializer.instance_variable_get(:@resource_name) || single.pluralize
39
-
40
- return %Q[{\"result\":#{MultiJson.dump(serializer)}}] if [serializer.object].flatten.size > 1
41
-
42
37
  serializer.root = false
43
- out = serializer.object.try(:empty?) ? nil : MultiJson.dump(serializer)
44
- if out.present? && out[0,1] == '[' && out[-1,1] == ']'
45
- # was probably a single mongoid criteria object or some enumerable that serialized itself into a json array
46
- out = out
47
- else
48
- # its a singular record, so we want to put it in an array
49
- out = "[#{out}]"
50
- end
51
- %Q[{\"result\":{\"#{plural}\":#{out}}}]
38
+ %Q[{\"result\":{\"#{pluralized_resource(serializer, env)}\":#{dump_to_json_array_string(serializer)}}}]
52
39
  else
53
- # Grape::Formatter::GarJsonSerializer.call serializer.object, env
54
40
  Grape::Formatter::GarJsonSerializer.call resource, env
55
41
  end
56
42
  end
43
+
44
+ private
45
+
46
+ def self.dump_to_json_array_string(serializer)
47
+ str = serializer.object.try(:empty?) ? nil : MultiJson.dump(serializer)
48
+ str = "[#{str}]" unless is_serialized_array_string?(str)
49
+ str
50
+ end
51
+
52
+ def self.is_serialized_array_string?(str)
53
+ str.present? && str[0,1] == '[' && str[-1,1] == ']'
54
+ end
55
+
56
+ def self.pluralized_resource(serializer, env)
57
+ single = serializer.try(:resource_singular) || serializer.instance_variable_get(:@resource_name).try(:singularize) || serializer.instance_variable_get(:@object).class.name.underscore
58
+ plural = serializer.try(:resource_plural) || serializer.instance_variable_get(:@resource_name) || single.pluralize
59
+ # allow the pluralized resource name to be overriden with a custom route setting
60
+ if env["rack.routing_args"][:route_info].route_settings[:custom] && env["rack.routing_args"][:route_info].route_settings[:custom][:resource_plural]
61
+ plural = env["rack.routing_args"][:route_info].route_settings[:custom][:resource_plural].to_s.downcase
62
+ end
63
+ plural
64
+ end
57
65
  end
58
66
 
59
67
  module GarRabl
@@ -1,3 +1,3 @@
1
1
  module GrapeApeRails
2
- VERSION = "0.9.10"
2
+ VERSION = "0.9.11"
3
3
  end
@@ -24,6 +24,12 @@ module API
24
24
  @monkey = Monkey.find(params[:id])
25
25
  @monkey
26
26
  end
27
+
28
+ desc "Get a single Monkey's bananas"
29
+ route_setting :custom, resource_plural: 'bananas'
30
+ get ':id/bananas' do
31
+ @bananas = Monkey.find(params[:id]).bananas
32
+ end
27
33
  end
28
34
  end
29
35
  end
@@ -0,0 +1,4 @@
1
+ class Banana < ActiveRecord::Base
2
+ validates_presence_of :size
3
+ belongs_to :monkey
4
+ end
@@ -0,0 +1,3 @@
1
+ class BananaSerializer < ActiveModel::Serializer
2
+ attributes :monkey_id, :size
3
+ end
@@ -1,3 +1,4 @@
1
1
  class Monkey < ActiveRecord::Base
2
2
  validates_presence_of :name, :color
3
+ has_many :bananas
3
4
  end
@@ -0,0 +1,8 @@
1
+ class CreateBananas < ActiveRecord::Migration
2
+ def change
3
+ create_table :bananas do |t|
4
+ t.string :size
5
+ t.references :monkey
6
+ end
7
+ end
8
+ end
@@ -11,7 +11,12 @@
11
11
  #
12
12
  # It's strongly recommended that you check this file into your version control system.
13
13
 
14
- ActiveRecord::Schema.define(version: 20140804173544) do
14
+ ActiveRecord::Schema.define(version: 20150514153831) do
15
+
16
+ create_table "bananas", force: true do |t|
17
+ t.string "size"
18
+ t.integer "monkey_id"
19
+ end
15
20
 
16
21
  create_table "monkeys", force: true do |t|
17
22
  t.string "name"
Binary file
@@ -1,6 +1,19 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe API::V1::Monkeys do
4
+ describe 'GET /monkeys/:id/bananas' do
5
+ let(:monkey) { FactoryGirl.create(:monkey_with_bananas) }
6
+ it 'succeeds' do
7
+ req :get, "/monkeys/#{monkey.id}/bananas"
8
+ expect(response.status).to eql 200
9
+ end
10
+ it 'returns the bananas in an array with key `bananas`' do
11
+ req :get, "/monkeys/#{monkey.id}/bananas"
12
+ expect(json_result.has_key?(:bananas)).to eql(true), "JSON expected to have key :bananas but it did not"
13
+ expect(json_result[:bananas].size).to eql(monkey.bananas.size)
14
+ end
15
+ end
16
+
4
17
  describe "GET /monkeys/:id" do
5
18
  let(:monkey) { FactoryGirl.create(:monkey) }
6
19
 
data/spec/spec_helper.rb CHANGED
@@ -61,9 +61,23 @@ FactoryGirl.define do
61
61
  rads 150
62
62
  end
63
63
 
64
+ factory :banana do
65
+ size "big"
66
+ monkey
67
+ end
68
+
64
69
  factory :monkey do
65
70
  name "Frank the Monkey"
66
71
  color "brown"
72
+
73
+ factory :monkey_with_bananas do
74
+ ignore do
75
+ bananas_count 5
76
+ end
77
+ after(:create) do |monkey, eval|
78
+ create_list(:banana, eval.bananas_count, monkey: monkey)
79
+ end
80
+ end
67
81
  end
68
82
  end
69
83
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape_ape_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.10
4
+ version: 0.9.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt E. Patterson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-05 00:00:00.000000000 Z
11
+ date: 2015-05-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -407,6 +407,8 @@ files:
407
407
  - spec/dummy/app/helpers/application_helper.rb
408
408
  - spec/dummy/app/mailers/.keep
409
409
  - spec/dummy/app/models/.keep
410
+ - spec/dummy/app/models/banana.rb
411
+ - spec/dummy/app/models/banana_serializer.rb
410
412
  - spec/dummy/app/models/concerns/.keep
411
413
  - spec/dummy/app/models/monkey.rb
412
414
  - spec/dummy/app/models/monkey_serializer.rb
@@ -441,6 +443,7 @@ files:
441
443
  - spec/dummy/db/development.sqlite3
442
444
  - spec/dummy/db/migrate/20140714154355_create_widgets.rb
443
445
  - spec/dummy/db/migrate/20140804173544_create_monkeys.rb
446
+ - spec/dummy/db/migrate/20150514153831_create_bananas.rb
444
447
  - spec/dummy/db/schema.rb
445
448
  - spec/dummy/db/test.sqlite3
446
449
  - spec/dummy/lib/assets/.keep
@@ -492,6 +495,8 @@ test_files:
492
495
  - spec/dummy/app/helpers/application_helper.rb
493
496
  - spec/dummy/app/mailers/.keep
494
497
  - spec/dummy/app/models/.keep
498
+ - spec/dummy/app/models/banana.rb
499
+ - spec/dummy/app/models/banana_serializer.rb
495
500
  - spec/dummy/app/models/concerns/.keep
496
501
  - spec/dummy/app/models/monkey.rb
497
502
  - spec/dummy/app/models/monkey_serializer.rb
@@ -526,6 +531,7 @@ test_files:
526
531
  - spec/dummy/db/development.sqlite3
527
532
  - spec/dummy/db/migrate/20140714154355_create_widgets.rb
528
533
  - spec/dummy/db/migrate/20140804173544_create_monkeys.rb
534
+ - spec/dummy/db/migrate/20150514153831_create_bananas.rb
529
535
  - spec/dummy/db/schema.rb
530
536
  - spec/dummy/db/test.sqlite3
531
537
  - spec/dummy/lib/assets/.keep