acts_as_api 0.4.3 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +4 -2
  3. data/History.txt +8 -0
  4. data/README.md +2 -2
  5. data/acts_as_api.gemspec +2 -2
  6. data/examples/introduction/index.html +6 -6
  7. data/examples/introduction/index.rb +1 -1
  8. data/examples/introduction/layout.mustache +4 -4
  9. data/lib/acts_as_api.rb +18 -2
  10. data/lib/acts_as_api/collection.rb +17 -0
  11. data/lib/acts_as_api/config.rb +15 -15
  12. data/lib/acts_as_api/rails_renderer.rb +6 -2
  13. data/lib/acts_as_api/rendering.rb +4 -4
  14. data/lib/acts_as_api/version.rb +1 -1
  15. data/spec/active_record_dummy/Gemfile +1 -1
  16. data/spec/active_record_dummy/app/models/profile.rb +2 -3
  17. data/spec/active_record_dummy/app/models/task.rb +2 -3
  18. data/spec/active_record_dummy/app/models/user.rb +6 -7
  19. data/spec/active_record_dummy/config/application.rb +0 -6
  20. data/spec/active_record_dummy/config/environments/development.rb +4 -7
  21. data/spec/active_record_dummy/config/environments/production.rb +0 -4
  22. data/spec/active_record_dummy/config/environments/test.rb +3 -3
  23. data/spec/controllers/plain_objects_controller_spec.rb +2 -1
  24. data/spec/controllers/respond_with_users_controller_spec.rb +8 -8
  25. data/spec/mongoid_dummy/Gemfile +2 -2
  26. data/spec/mongoid_dummy/config/application.rb +0 -7
  27. data/spec/mongoid_dummy/config/environments/development.rb +4 -0
  28. data/spec/mongoid_dummy/config/environments/test.rb +3 -1
  29. data/spec/mongoid_dummy/config/mongoid.yml +4 -112
  30. data/spec/shared_engine/app/controllers/shared_engine/plain_objects_controller.rb +1 -1
  31. data/spec/shared_engine/app/controllers/shared_engine/respond_with_users_controller.rb +6 -6
  32. data/spec/shared_engine/app/controllers/shared_engine/users_controller.rb +8 -4
  33. data/spec/shared_engine/dummy/config/application.rb +0 -6
  34. data/spec/shared_engine/dummy/config/environments/development.rb +4 -7
  35. data/spec/shared_engine/dummy/config/environments/production.rb +0 -4
  36. data/spec/shared_engine/dummy/config/environments/test.rb +3 -3
  37. data/spec/shared_engine/shared_engine.gemspec +1 -1
  38. data/spec/spec_helper.rb +7 -5
  39. data/spec/support/controller_examples.rb +23 -47
  40. data/spec/support/model_examples/associations.rb +59 -59
  41. metadata +4 -8
  42. data/lib/acts_as_api/array.rb +0 -21
  43. data/spec/shared_engine/lib/magic/rails/engine.rb +0 -69
  44. data/spec/support/routing.rb +0 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 750c497db915d3d79eb40810cdaf5913c9444fa9
4
- data.tar.gz: e30520e560f3381f58f683784cf906f910a1bdcc
3
+ metadata.gz: f83008775d02827f67246d2c3d6de3611318a279
4
+ data.tar.gz: 8827a13005d4caaa92561987fa503feea450be86
5
5
  SHA512:
6
- metadata.gz: 0ef61479b5003497864d5849ac686e07afea4a4cbf40d321beb40e9ef5446cc79945f46578a0c4b7938b3f3987d4ff68499700f935a18d9b3c0e2b2515875a72
7
- data.tar.gz: ad9dbf1e35bac88887795d19f414fd7eef6d7a5113e0e00fb04ecdf95158a15f74e3cbc0ec788e3b263a77c941dd452e3a6e1c9cddbb081b89c5bb08a5a52fc8
6
+ metadata.gz: 8710cf02573c85ca1a902b2d48d42955c251058ac4f01eeb95a26bae402a9158fe8032ed52c82990ffd8cb9d0ec4bbb45a027fd5d2285fd99157585ff8626e29
7
+ data.tar.gz: 875d70c8a6ccb93fe4ed2eda2a6a565ae87fca28c37fd8e87e82c360c66ee1a74c113c0bdac5d65b6b917326e31e58e78866d481f0b8462812f3ba3eb4282a43
data/Gemfile CHANGED
@@ -4,14 +4,16 @@ source "http://rubygems.org"
4
4
  gemspec
5
5
 
6
6
  group :test do
7
+ gem 'pry'
7
8
  gem 'test-unit', '~> 3.0'
8
- gem 'sqlite3-ruby'
9
- gem 'mongoid', '>= 3.0.1'
9
+ gem 'sqlite3'
10
+ gem 'mongoid', '>= 6.0.2'
10
11
  gem 'rspec', '>= 2.9.0'
11
12
  gem 'rspec-its'
12
13
  gem 'rspec-collection_matchers'
13
14
  gem 'capybara'
14
15
  gem 'rspec-rails', '>= 2.5.0'
15
16
  gem 'webrat'
17
+ gem 'responders'
16
18
  gem 'shared_engine', :path => './spec/shared_engine'
17
19
  end
@@ -1,3 +1,11 @@
1
+ === 0.4.4 2016-11-09
2
+
3
+ * Updates spec suite to Rails 5
4
+
5
+ === 0.4.3 2016-07-31
6
+
7
+ * Sets correct content type for JSONP responses (see PR #98)
8
+
1
9
  === 0.4.2 2013-12-28
2
10
 
3
11
  * Fixed problem with rendering of POROs (see PR #88)
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # acts_as_api ![acts_as_api on travis ci](https://secure.travis-ci.org/fabrik42/acts_as_api.png?branch=master)
2
2
 
3
- acts_as_api makes creating XML/JSON responses in Rails 3 easy and fun.
3
+ acts_as_api makes creating XML/JSON responses in Rails 3, 4 and 5 easy and fun.
4
4
 
5
5
  It provides a simple interface to determine the representation of your model data, that should be rendered in your API responses.
6
6
 
@@ -55,7 +55,7 @@ There are a lot of how-tos like:
55
55
 
56
56
  * DRY templates for your api responses
57
57
  * Ships with support for __ActiveRecord__ and __Mongoid__
58
- * Support for Rails 3 Responders
58
+ * Support for Rails 3/4 Responders (extracted to responders gem since Rails 5)
59
59
  * Plays very well together with client libs like [Backbone.js](http://documentcloud.github.com/backbone), [RestKit](http://restkit.org) (iOS) or [gson](http://code.google.com/p/google-gson) (Android).
60
60
  * Easy but very flexible syntax for defining the templates
61
61
  * XML, JSON and JSON-P support out of the box, easy to extend
@@ -1,4 +1,4 @@
1
- # encoding: UTF-8
1
+ # encoding: utf-8
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
3
  require "acts_as_api/version"
4
4
 
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
9
9
  s.authors = ["Christian Bäuerlein"]
10
10
  s.email = ["christian@ffwdme.com"]
11
11
  s.homepage = "https://github.com/fabrik42/acts_as_api"
12
- s.summary = %q{Makes creating XML/JSON responses in Rails 3 easy and fun.}
12
+ s.summary = %q{Makes creating XML/JSON responses in Rails 3, 4 and 5 easy and fun.}
13
13
  s.description = %q{acts_as_api enriches the models and controllers of your app in a rails-like way so you can easily determine how your XML/JSON API responses should look like.}
14
14
 
15
15
  s.add_dependency('activemodel','>= 3.0.0')
@@ -5,7 +5,7 @@
5
5
  <title>acts_as_api</title>
6
6
  <meta content="A Ruby/Rails gem to easily generate web api reponses!" name="description" />
7
7
  <meta content="Christian Bäuerlein" name="author" />
8
- <meta content="en" name="language" />
8
+ <meta content="en" name="language" />
9
9
  <link rel="stylesheet" href="./docco.css">
10
10
  <link href="http://fonts.googleapis.com/css?family=Copse:regular" rel="stylesheet" type="text/css" >
11
11
  <style>
@@ -20,7 +20,7 @@
20
20
  word-spacing: 0em;
21
21
  line-height: 1.2;
22
22
  }
23
- </style>
23
+ </style>
24
24
  </head>
25
25
  <body>
26
26
  <div id='container'>
@@ -30,7 +30,7 @@
30
30
  <tr>
31
31
  <th class=docs>
32
32
  <h1>acts_as_api</h1>
33
- <p>Makes creating XML/JSON responses in Rails 3 easy and fun.</p>
33
+ <p>Makes creating XML/JSON responses in Rails 3, 4 and 5 easy and fun.</p>
34
34
  </th>
35
35
  <th class=code></th>
36
36
  </tr>
@@ -60,7 +60,7 @@ You surely don’t want to expose your models always with all attributes.</p>
60
60
  <ul>
61
61
  <li>DRY templates for your api responses</li>
62
62
  <li>Ships with support for <strong>ActiveRecord</strong> and <strong>Mongoid</strong></li>
63
- <li>Support for Rails 3 Responders</li>
63
+ <li>Support for Rails 3/4 Responders (extracted to responders gem since Rails 5)</li>
64
64
  <li>Plays very well together with client libs like <a href="http://documentcloud.github.com/backbone">Backbone.js</a> or <a href="http://restkit.org">RestKit</a> (iOS).</li>
65
65
  <li>Easy but very flexible syntax for defining the templates</li>
66
66
  <li>XML, JSON and JSON-P support out of the box, easy to extend</li>
@@ -81,7 +81,7 @@ You surely don’t want to expose your models always with all attributes.</p>
81
81
  <div class="pilwrap">
82
82
  <a class="pilcrow" href="#section-Rails_3.x_Quickstart">&#182;</a>
83
83
  </div>
84
- <h2>Rails 3.x Quickstart</h2>
84
+ <h2>Rails Quickstart</h2>
85
85
  </td>
86
86
  <td class=code>
87
87
  <div class='highlight'><pre></pre></div>
@@ -335,4 +335,4 @@ provides you some tools to customize your API responses.</p>
335
335
  </table>
336
336
  </div>
337
337
  <a href="https://github.com/fabrik42/acts_as_api"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://assets1.github.com/img/7afbc8b248c68eb468279e8c17986ad46549fb71?repo=&url=http%3A%2F%2Fs3.amazonaws.com%2Fgithub%2Fribbons%2Fforkme_right_darkblue_121621.png&path=" alt="Fork me on GitHub"></a>
338
- </body>
338
+ </body>
@@ -6,7 +6,7 @@
6
6
  ### Features
7
7
  # * DRY templates for your api responses
8
8
  # * Ships with support for **ActiveRecord** and **Mongoid**
9
- # * Support for Rails 3 Responders
9
+ # * Support for Rails 3, 4 and 5 Responders
10
10
  # * Plays very well together with client libs like [Backbone.js][b1] or [RestKit][r1] (iOS).
11
11
  # * Easy but very flexible syntax for defining the templates
12
12
  # * XML, JSON and JSON-P support out of the box, easy to extend
@@ -5,7 +5,7 @@
5
5
  <title>acts_as_api</title>
6
6
  <meta content="A Ruby/Rails gem to easily generate web api reponses!" name="description" />
7
7
  <meta content="Christian Bäuerlein" name="author" />
8
- <meta content="en" name="language" />
8
+ <meta content="en" name="language" />
9
9
  <link rel="stylesheet" href="./docco.css">
10
10
  <link href="http://fonts.googleapis.com/css?family=Copse:regular" rel="stylesheet" type="text/css" >
11
11
  <style>
@@ -20,7 +20,7 @@
20
20
  word-spacing: 0em;
21
21
  line-height: 1.2;
22
22
  }
23
- </style>
23
+ </style>
24
24
  </head>
25
25
  <body>
26
26
  <div id='container'>
@@ -42,7 +42,7 @@
42
42
  <tr>
43
43
  <th class=docs>
44
44
  <h1>acts_as_api</h1>
45
- <p>Makes creating XML/JSON responses in Rails 3 easy and fun.</p>
45
+ <p>Makes creating XML/JSON responses in Rails 3, 4 and 5 easy and fun.</p>
46
46
  </th>
47
47
  <th class=code></th>
48
48
  </tr>
@@ -64,4 +64,4 @@
64
64
  </table>
65
65
  </div>
66
66
  <a href="https://github.com/fabrik42/acts_as_api"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://assets1.github.com/img/7afbc8b248c68eb468279e8c17986ad46549fb71?repo=&url=http%3A%2F%2Fs3.amazonaws.com%2Fgithub%2Fribbons%2Fforkme_right_darkblue_121621.png&path=" alt="Fork me on GitHub"></a>
67
- </body>
67
+ </body>
@@ -4,12 +4,12 @@ require 'active_support/core_ext/class'
4
4
  $:.unshift(File.dirname(__FILE__)) unless
5
5
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
6
6
 
7
- require "acts_as_api/array"
7
+ require "acts_as_api/collection"
8
8
  require "acts_as_api/rails_renderer"
9
9
  require "acts_as_api/exceptions"
10
10
 
11
11
  # acts_as_api is a gem that aims to make the construction of JSON and XML
12
- # responses in rails 3 easy and fun.
12
+ # responses in Rails 3, 4 and 5 easy and fun.
13
13
  #
14
14
  # Therefore it attaches a couple of helper methods to active record and
15
15
  # the action controller base classes.
@@ -25,9 +25,25 @@ module ActsAsApi
25
25
  autoload :Adapters, "acts_as_api/adapters"
26
26
  end
27
27
 
28
+ # Neccessary to render an Array of models, e.g. the result of a search.
29
+ Array.include(ActsAsApi::Collection)
30
+
28
31
  # Attach ourselves to ActiveRecord
29
32
  if defined?(ActiveRecord::Base)
30
33
  ActiveRecord::Base.extend ActsAsApi::Base
34
+
35
+ # Rails 5 compatibility
36
+ if defined?(ActiveRecord::Relation)
37
+ ActiveRecord::Relation.include(ActsAsApi::Collection)
38
+ end
39
+
40
+ if defined?(ActiveRecord::Associations::CollectionProxy)
41
+ ActiveRecord::Associations::CollectionProxy.include(ActsAsApi::Collection)
42
+ end
43
+
44
+ if defined?(ActiveRecord::AssociationRelation)
45
+ ActiveRecord::AssociationRelation.include(ActsAsApi::Collection)
46
+ end
31
47
  end
32
48
 
33
49
  # Attach ourselves to ActiveResource
@@ -0,0 +1,17 @@
1
+ module ActsAsApi
2
+ module Collection
3
+ # The collection checks all its items if they respond to the +as_api_response+ method.
4
+ # If they do, the result of this method will be collected.
5
+ # If they don't, the item itself will be collected.
6
+ def as_api_response(api_template, options = {})
7
+ collect do |item|
8
+ if item.respond_to?(:as_api_response)
9
+ item.as_api_response(api_template,options)
10
+ else
11
+ item
12
+ end
13
+ end
14
+ end
15
+
16
+ end
17
+ end
@@ -1,58 +1,58 @@
1
1
  module ActsAsApi
2
2
 
3
3
  module Config
4
-
4
+
5
5
  class << self
6
6
 
7
7
  attr_writer :accepted_api_formats, :dasherize_for, :include_root_in_json_collections, :add_root_node_for, :default_root, :allow_jsonp_callback, :add_http_status_to_jsonp_response
8
-
8
+
9
9
  # The accepted response formats
10
10
  # Default is <tt>[:xml, :json]</tt>
11
11
  def accepted_api_formats
12
12
  @accepted_api_formats || [:xml, :json]
13
13
  end
14
-
15
- # Holds formats that should be dasherized
16
- # Default is <tt>[:xml]</tt>
14
+
15
+ # Holds formats that should be dasherized
16
+ # Default is <tt>[:xml]</tt>
17
17
  def dasherize_for
18
18
  @dasherize_for || [:xml]
19
- end
20
-
19
+ end
20
+
21
21
  # If true, the root node in json collections will be added
22
22
  # so the response will look like the default Rails 3 json
23
23
  # response
24
24
  def include_root_in_json_collections
25
25
  @include_root_in_json_collections || false
26
26
  end
27
-
27
+
28
28
  # Holds references to formats that need
29
29
  # to get added an additional root node
30
30
  # with the name of the model.
31
31
  def add_root_node_for
32
32
  @add_root_node_for || [:json]
33
- end
34
-
33
+ end
34
+
35
35
  # The default name of a root node of a response
36
36
  # if no root paramter is passed in render_for_api
37
37
  # and the gem is not able to determine a root name
38
- # automatically
38
+ # automatically
39
39
  def default_root
40
40
  @default_root || :record
41
- end
42
-
41
+ end
42
+
43
43
  # If true a json response will be automatically wrapped into
44
44
  # a JavaScript function call in case a :callback param is passed.
45
45
  def allow_jsonp_callback
46
46
  @allow_jsonp_callback || false
47
47
  end
48
-
48
+
49
49
  # If true the jsonp function call will get the http status passed
50
50
  # as a second parameter
51
51
  def add_http_status_to_jsonp_response
52
52
  @add_http_status_to_jsonp_response.nil? ? true : @add_http_status_to_jsonp_response
53
53
  end
54
54
  end
55
-
55
+
56
56
  end
57
57
 
58
58
  end
@@ -7,8 +7,12 @@ module ActsAsApi
7
7
  def self.setup
8
8
  ActionController.add_renderer :acts_as_api_jsonp do |json, options|
9
9
  json = ActiveSupport::JSON.encode(json) unless json.respond_to?(:to_str)
10
- json = "#{options[:callback]}(#{json}, #{response.status})" unless options[:callback].blank?
11
- self.content_type ||= options[:callback].blank ? Mime::JSON : Mime::JS
10
+
11
+ if options[:callback].present?
12
+ json = "#{options[:callback]}(#{json}, #{response.status})"
13
+ self.content_type = Mime[:js]
14
+ end
15
+
12
16
  self.response_body = json
13
17
  end
14
18
  end
@@ -41,8 +41,8 @@ module ActsAsApi
41
41
 
42
42
  if !output_params[:root].nil?
43
43
  api_root_name = output_params[:root].to_s
44
- elsif api_model.class.respond_to?(:model_name)
45
- api_root_name = api_model.class.model_name
44
+ elsif api_model.class.respond_to?(:model_name)
45
+ api_root_name = api_model.class.model_name
46
46
  elsif api_model.respond_to?(:collection_name)
47
47
  api_root_name = api_model.collection_name
48
48
  elsif api_model.is_a?(Array) && !api_model.empty? && api_model.first.class.respond_to?(:model_name)
@@ -50,7 +50,7 @@ module ActsAsApi
50
50
  elsif api_model.is_a?(Array) && !api_model.empty? && api_model.first.respond_to?(:model_name)
51
51
  api_root_name = api_model.first.model_name
52
52
  elsif api_model.respond_to?(:model_name)
53
- api_root_name = api_model.model_name
53
+ api_root_name = api_model.model_name
54
54
  else
55
55
  api_root_name = ActsAsApi::Config.default_root.to_s
56
56
  end
@@ -78,7 +78,7 @@ module ActsAsApi
78
78
  end
79
79
 
80
80
  api_response = meta_hash.merge api_response if meta_hash
81
-
81
+
82
82
  if ActsAsApi::Config.allow_jsonp_callback && params[:callback]
83
83
  output_params[:callback] = params[:callback]
84
84
  api_format = :acts_as_api_jsonp if ActsAsApi::Config.add_http_status_to_jsonp_response
@@ -1,3 +1,3 @@
1
1
  module ActsAsApi
2
- VERSION = "0.4.3"
2
+ VERSION = "0.4.4"
3
3
  end
@@ -1,6 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'rails', '3.2.3'
3
+ gem 'rails', '5.0.0.1'
4
4
 
5
5
  # Bundle edge Rails instead:
6
6
  # gem 'rails', :git => 'git://github.com/rails/rails.git'
@@ -1,4 +1,3 @@
1
1
  class Profile < ActiveRecord::Base
2
- attr_accessible :avatar, :homepage
3
- belongs_to :user
4
- end
2
+ belongs_to :user
3
+ end
@@ -1,4 +1,3 @@
1
1
  class Task < ActiveRecord::Base
2
- attr_accessible :heading, :description, :time_spent, :done
3
- belongs_to :user
4
- end
2
+ belongs_to :user
3
+ end
@@ -1,5 +1,4 @@
1
1
  class User < ActiveRecord::Base
2
- attr_accessible :first_name, :last_name, :age, :active
3
2
  validates :first_name, :last_name, :presence => true
4
3
 
5
4
  has_many :tasks
@@ -131,27 +130,27 @@ class User < ActiveRecord::Base
131
130
  t.add :last_name
132
131
  t.add lambda{|model| 'true' }, :as => :postfix
133
132
  end
134
-
133
+
135
134
  def before_api_response(api_response)
136
135
  @before_api_response_called = true
137
136
  end
138
-
137
+
139
138
  def before_api_response_called?
140
139
  !!@before_api_response_called
141
140
  end
142
-
141
+
143
142
  def after_api_response(api_response)
144
143
  @after_api_response_called = true
145
144
  end
146
-
145
+
147
146
  def after_api_response_called?
148
147
  !!@after_api_response_called
149
148
  end
150
-
149
+
151
150
  def skip_api_response=(should_skip)
152
151
  @skip_api_response = should_skip
153
152
  end
154
-
153
+
155
154
  def around_api_response(api_response)
156
155
  @skip_api_response ? { :skipped => true } : yield
157
156
  end
@@ -44,12 +44,6 @@ module ActiveRecordDummy
44
44
  # like if you have constraints or database-specific column types
45
45
  # config.active_record.schema_format = :sql
46
46
 
47
- # Enforce whitelist mode for mass assignment.
48
- # This will create an empty whitelist of attributes available for mass-assignment for all models
49
- # in your app. As such, your models will need to explicitly whitelist or blacklist accessible
50
- # parameters by using an attr_accessible or attr_protected declaration.
51
- config.active_record.whitelist_attributes = true
52
-
53
47
  # Enable the asset pipeline
54
48
  config.assets.enabled = true
55
49
 
@@ -22,16 +22,13 @@ ActiveRecordDummy::Application.configure do
22
22
  # Only use best-standards-support built into browsers
23
23
  config.action_dispatch.best_standards_support = :builtin
24
24
 
25
- # Raise exception on mass assignment protection for Active Record models
26
- config.active_record.mass_assignment_sanitizer = :strict
27
-
28
- # Log the query plan for queries taking more than this (works
29
- # with SQLite, MySQL, and PostgreSQL)
30
- config.active_record.auto_explain_threshold_in_seconds = 0.5
31
-
32
25
  # Do not compress assets
33
26
  config.assets.compress = false
34
27
 
28
+ config.eager_load = false
29
+
30
+ config.secret_key_base = 'helloworld123'
31
+
35
32
  # Expands the lines which load the assets
36
33
  config.assets.debug = true
37
34
  end