Skip to content

Commit 42a3c64

Browse files
committed
Merge pull request #286 from cerebris/pr/285
Rebase #285 - updated to Ruby Style Guide Practices
2 parents 7e729bd + 8e7dc1f commit 42a3c64

24 files changed

Lines changed: 391 additions & 471 deletions

Gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ end
1313
version = ENV['RAILS_VERSION'] || 'default'
1414
rails = case version
1515
when 'master'
16-
{:github => 'rails/rails'}
16+
{ github: 'rails/rails' }
1717
when 'default'
18-
'>= 4.2'
18+
'>= 4.2'
1919
else
2020
"~> #{version}"
2121
end

Rakefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env rake
2-
require "bundler/gem_tasks"
3-
require "rake/testtask"
2+
require 'bundler/gem_tasks'
3+
require 'rake/testtask'
44
require './test/test_helper.rb'
55

66
TestApp.load_tasks

jsonapi-resources.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
88
spec.version = JSONAPI::Resources::VERSION
99
spec.authors = ['Dan Gebhardt', 'Larry Gebhardt']
1010
spec.email = ['dan@cerebris.com', 'larry@cerebris.com']
11-
spec.summary = %q{Easily support JSON API in Rails.}
12-
spec.description = %q{A resource-centric approach to implementing the controllers, routes, and serializers needed to support the JSON API spec.}
11+
spec.summary = 'Easily support JSON API in Rails.'
12+
spec.description = 'A resource-centric approach to implementing the controllers, routes, and serializers needed to support the JSON API spec.'
1313
spec.homepage = 'https://github.com/cerebris/jsonapi-resources'
1414
spec.license = 'MIT'
1515

lib/jsonapi-resources.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,3 @@
2020
require 'jsonapi/operation_result'
2121
require 'jsonapi/operation_results'
2222
require 'jsonapi/callbacks'
23-

lib/jsonapi/active_record_operations_processor.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class ActiveRecordOperationsProcessor < JSONAPI::OperationsProcessor
2-
32
private
3+
44
def transaction
55
if @transactional
66
ActiveRecord::Base.transaction do
@@ -12,7 +12,7 @@ def transaction
1212
end
1313

1414
def rollback
15-
raise ActiveRecord::Rollback if @transactional
15+
fail ActiveRecord::Rollback if @transactional
1616
end
1717

1818
def process_operation(operation)

lib/jsonapi/acts_as_resource_controller.rb

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def create_operations_processor
6060
end
6161

6262
private
63+
6364
def resource_klass
6465
@resource_klass ||= resource_klass_name.safe_constantize
6566
end
@@ -78,17 +79,15 @@ def resource_klass_name
7879

7980
def ensure_correct_media_type
8081
unless request.content_type == JSONAPI::MEDIA_TYPE
81-
raise JSONAPI::Exceptions::UnsupportedMediaTypeError.new(request.content_type)
82+
fail JSONAPI::Exceptions::UnsupportedMediaTypeError.new(request.content_type)
8283
end
8384
rescue => e
8485
handle_exceptions(e)
8586
end
8687

8788
def setup_request
88-
@request = JSONAPI::Request.new(params, {
89-
context: context,
90-
key_formatter: key_formatter
91-
})
89+
@request = JSONAPI::Request.new(params, context: context,
90+
key_formatter: key_formatter)
9291
render_errors(@request.errors) unless @request.errors.empty?
9392
rescue => e
9493
handle_exceptions(e)
@@ -128,7 +127,7 @@ def base_response_links
128127
end
129128

130129
def render_errors(errors)
131-
operation_results = JSONAPI::OperationResults.new()
130+
operation_results = JSONAPI::OperationResults.new
132131
result = JSONAPI::ErrorsOperationResult.new(errors[0].status, errors)
133132
operation_results.add_result(result)
134133

@@ -143,18 +142,16 @@ def render_results(operation_results)
143142
def create_response_document(operation_results)
144143
JSONAPI::ResponseDocument.new(
145144
operation_results,
146-
{
147-
primary_resource_klass: resource_klass,
148-
include_directives: @request ? @request.include_directives : nil,
149-
fields: @request ? @request.fields : nil,
150-
base_url: base_url,
151-
key_formatter: key_formatter,
152-
route_formatter: route_formatter,
153-
base_meta: base_response_meta,
154-
base_links: base_response_links,
155-
resource_serializer_klass: resource_serializer_klass,
156-
request: @request
157-
}
145+
primary_resource_klass: resource_klass,
146+
include_directives: @request ? @request.include_directives : nil,
147+
fields: @request ? @request.fields : nil,
148+
base_url: base_url,
149+
key_formatter: key_formatter,
150+
route_formatter: route_formatter,
151+
base_meta: base_response_meta,
152+
base_links: base_response_links,
153+
resource_serializer_klass: resource_serializer_klass,
154+
request: @request
158155
)
159156
end
160157

@@ -169,11 +166,11 @@ def process_request_operations
169166
# Note: Be sure to either call super(e) or handle JSONAPI::Exceptions::Error and raise unhandled exceptions
170167
def handle_exceptions(e)
171168
case e
172-
when JSONAPI::Exceptions::Error
173-
render_errors(e.errors)
174-
else # raise all other exceptions
175-
# :nocov:
176-
raise e
169+
when JSONAPI::Exceptions::Error
170+
render_errors(e.errors)
171+
else # raise all other exceptions
172+
# :nocov:
173+
fail e
177174
# :nocov:
178175
end
179176
end

lib/jsonapi/association.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ module JSONAPI
22
class Association
33
attr_reader :acts_as_set, :foreign_key, :type, :options, :name, :class_name
44

5-
def initialize(name, options={})
5+
def initialize(name, options = {})
66
@name = name.to_s
77
@options = options
88
@acts_as_set = options.fetch(:acts_as_set, false) == true
9-
@foreign_key = options[:foreign_key ] ? options[:foreign_key ].to_sym : nil
10-
@module_path = options.fetch(:module_path, '')
9+
@foreign_key = options[:foreign_key] ? options[:foreign_key].to_sym : nil
10+
@module_path = options[:module_path] || ''
1111
@relation_name = options.fetch(:relation_name, @name)
1212
end
1313

@@ -33,7 +33,7 @@ def relation_name(options = {})
3333
end
3434

3535
class HasOne < Association
36-
def initialize(name, options={})
36+
def initialize(name, options = {})
3737
super
3838
@class_name = options.fetch(:class_name, name.to_s.camelize)
3939
@type = class_name.underscore.pluralize.to_sym
@@ -42,11 +42,11 @@ def initialize(name, options={})
4242
end
4343

4444
class HasMany < Association
45-
def initialize(name, options={})
45+
def initialize(name, options = {})
4646
super
4747
@class_name = options.fetch(:class_name, name.to_s.camelize.singularize)
4848
@type = class_name.underscore.pluralize.to_sym
49-
@foreign_key ||= "#{name.to_s.singularize}_ids".to_sym
49+
@foreign_key ||= "#{name.to_s.singularize}_ids".to_sym
5050
end
5151
end
5252
end

lib/jsonapi/callbacks.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
module JSONAPI
44
module Callbacks
5-
65
def self.included(base)
76
base.class_eval do
87
include ActiveSupport::Callbacks
@@ -49,4 +48,4 @@ def _define_after_callback(klass, callback) #:nodoc:
4948
end
5049
end
5150
end
52-
end
51+
end

lib/jsonapi/configuration.rb

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -62,37 +62,21 @@ def operations_processor=(operations_processor)
6262
@operations_processor = JSONAPI::OperationsProcessor.operations_processor_for(@operations_processor_name)
6363
end
6464

65-
def allowed_request_params=(allowed_request_params)
66-
@allowed_request_params = allowed_request_params
67-
end
65+
attr_writer :allowed_request_params
6866

69-
def default_paginator=(default_paginator)
70-
@default_paginator = default_paginator
71-
end
67+
attr_writer :default_paginator
7268

73-
def default_page_size=(default_page_size)
74-
@default_page_size = default_page_size
75-
end
69+
attr_writer :default_page_size
7670

77-
def maximum_page_size=(maximum_page_size)
78-
@maximum_page_size = maximum_page_size
79-
end
71+
attr_writer :maximum_page_size
8072

81-
def use_text_errors=(use_text_errors)
82-
@use_text_errors = use_text_errors
83-
end
73+
attr_writer :use_text_errors
8474

85-
def top_level_links_include_pagination=(top_level_links_include_pagination)
86-
@top_level_links_include_pagination = top_level_links_include_pagination
87-
end
75+
attr_writer :top_level_links_include_pagination
8876

89-
def top_level_meta_include_record_count=(top_level_meta_include_record_count)
90-
@top_level_meta_include_record_count = top_level_meta_include_record_count
91-
end
77+
attr_writer :top_level_meta_include_record_count
9278

93-
def top_level_meta_record_count_key=(top_level_meta_record_count_key)
94-
@top_level_meta_record_count_key = top_level_meta_record_count_key
95-
end
79+
attr_writer :top_level_meta_record_count_key
9680
end
9781

9882
class << self

lib/jsonapi/error.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
module JSONAPI
22
class Error
3-
43
attr_accessor :title, :detail, :id, :href, :code, :path, :links, :status
54

6-
def initialize(options={})
5+
def initialize(options = {})
76
@title = options[:title]
87
@detail = options[:detail]
98
@id = options[:id]

0 commit comments

Comments
 (0)