forked from PositiveControl/params_deserializers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request PositiveControl#3 from pjdavis/rails-5-generator
Add a generator for creating deserializers in the rails project.
- Loading branch information
Showing
8 changed files
with
80 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Description: | ||
Generates a deserializer for the given resource. | ||
|
||
Example: | ||
`rails generate params_deserializer User first_name last_name birthday` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Rails | ||
module Generators | ||
class ParamsDeserializerGenerator < NamedBase | ||
source_root File.expand_path('../templates', __FILE__) | ||
check_class_collision suffix: 'ParamsDeserializer' | ||
|
||
argument :attributes, type: :array, default: [], banner: 'field field' | ||
|
||
def create_serializer_file | ||
template 'deserializer.rb.erb', File.join('app/deserializers', class_path, "#{file_name}_params_deserializer.rb") | ||
end | ||
|
||
private | ||
|
||
def attributes_names | ||
attributes.map! { |a| a.name.to_sym } | ||
end | ||
|
||
def parent_class_name | ||
'ParamsDeserializer' | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require 'rails/generators' | ||
require 'rails/generators/rails/resource/resource_generator' | ||
|
||
module Rails | ||
module Generators | ||
class ResourceGenerator | ||
hook_for :params_deserializer, default: true, type: :boolean | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<% module_namespacing do -%> | ||
class <%= class_name %>ParamsDeserializer < <%= parent_class_name %> | ||
attributes <%= attributes_names.map(&:inspect).join(", ") %> | ||
end | ||
<% end -%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,5 +28,5 @@ | |
# POSSIBILITY OF SUCH DAMAGE. | ||
|
||
module ParamsDeserializers | ||
VERSION = "2.0.0" | ||
VERSION = "2.1.0" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
spec/generators/rails/params_deserailizer_generator_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require "generator_spec" | ||
require_relative '../../../lib/generators/rails/params_deserializer_generator' | ||
describe "Rails::Generators::ParamsDeserializerGenerator", type: :generator do | ||
tests Rails::Generators::ParamsDeserializerGenerator | ||
destination File.expand_path("../../tmp", __FILE__) | ||
arguments %w(User birthday) | ||
|
||
before(:each) do | ||
prepare_destination | ||
run_generator | ||
end | ||
|
||
after(:each) do | ||
remove_entry_secure(destination_root) | ||
end | ||
|
||
it "creates a user deserializer" do | ||
expect(destination_root).to have_structure { | ||
directory "app" do | ||
directory "deserializers" do | ||
file "user_params_deserializer.rb" do | ||
contains "UserParamsDeserializer < ParamsDeserializer" | ||
end | ||
end | ||
end | ||
} | ||
end | ||
end |