blueprinter 0.21.0 → 0.22.0

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
  SHA256:
3
- metadata.gz: c8c9a52221d1a5473708cc7391865c25400d9e34440a1c0c52d76f2e7c811997
4
- data.tar.gz: 380008b99bfd079c13d3c8ce019e4160b92141c20c639b233adaa6ef1ccfc9e5
3
+ metadata.gz: 335ed3cf4448c0972904b81679e8e95304ab075064c1022555062b8eb07eaf12
4
+ data.tar.gz: fd442388bb18481015b4ae8ed79ca9b79b9a34f6eef6f5b31eb9ef473c458287
5
5
  SHA512:
6
- metadata.gz: 225d6b77da9cf47411b014372234e7091d52d6a8ce0d358cc01d6f4a9eb188eafab70a50295a9e9ba3b15863d91cc35a3e3dcdcf501221ca8c959c785420e064
7
- data.tar.gz: 0f407b0d11bcce8176c8e31f15e16f497431ffffe637ffe35e03779e91335902d70ef778546f55913b18885a261a9472fadc81b62caef490e64eb02d07900f5f
6
+ metadata.gz: 6855988c4693065d2620a19995f326718fd14796b0ec856b1ace0d5e91068959c72ada4788881e4b7bc2fbe587f56cf146dba06817431feaa6fb7d487d2d1eba
7
+ data.tar.gz: 4aef0b54eea76568616e860863d782caced131399cdd36bdec2874f5b701b139517367556fe880f5041b7acd4fe6619eba79f8c7180c8fe8b31f09bd279cdc5d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 0.22.0 - 2019/12/26
2
+ * 🚀 [FEATURE] Add rails generators. See `rails g blueprinter:blueprint --help` for usage. Introduced in [#176](https://github.com/procore/blueprinter/pull/176) by [@wlkrw](https://github.com/wlkrw).
3
+
1
4
  ## 0.21.0 - 2019/12/19
2
5
  * 🚀 [FEATURE] Ability to specify `default_if` field/association option for more control on when the default value is applied. [191](https://github.com/procore/blueprinter/pull/191). Thanks to [@mcclayton](https://github.com/mcclayton).
3
6
 
@@ -1,3 +1,3 @@
1
1
  module Blueprinter
2
- VERSION = '0.21.0'.freeze
2
+ VERSION = '0.22.0'.freeze
3
3
  end
@@ -0,0 +1,127 @@
1
+ module Blueprinter
2
+ module Generators
3
+ class BlueprintGenerator < ::Rails::Generators::NamedBase
4
+ desc "Generates blueprint for ActiveRecord model with the given NAME."
5
+
6
+ attr_accessor :options
7
+
8
+ source_root File.expand_path("../templates", __FILE__)
9
+
10
+
11
+
12
+ class_option :blueprints_dir, default: "app/blueprints", desc: "path to new blueprint", aliases: "-d"
13
+
14
+
15
+
16
+ class_option :identifier, default: nil, desc: "Add an identifer to the generated blueprint, either uses :id or your specified value", aliases: "-i", banner: "id"
17
+
18
+
19
+
20
+ class_option :fields, type: :array, default: [], desc: "Manually add specified fields"
21
+
22
+ class_option :detect_fields, type: :boolean, default: false, desc: "Introspect on the model to set fields in the generated blueprint. Will be merged with any manually specified"
23
+
24
+
25
+
26
+ class_option :associations, type: :array, default: [], desc: "Manually add specified associations", aliases: "-a"
27
+
28
+ class_option :detect_associations, type: :boolean, default: false, desc: "Introspect on the model to set associations in the generated blueprint. Will be merged with any manually specified"
29
+
30
+
31
+
32
+ class_option :wrap_at, type: :numeric, default: 80, desc: "Maximum length of generated fields line", aliases: "-w"
33
+
34
+ class_option :indentation, type: :string, default: "two", desc: "Indentation of generated file", banner: "two|four|tab"
35
+
36
+
37
+
38
+ remove_class_option :skip_namespace
39
+
40
+ def ensure_blueprint_dir
41
+ FileUtils.mkdir_p(path) unless File.directory?(path)
42
+ end
43
+
44
+ def create_blueprint
45
+ template "blueprint.rb", File.join(path, "#{file_path}_blueprint.rb")
46
+ end
47
+
48
+
49
+
50
+ private
51
+
52
+ def path
53
+ options["blueprints_dir"]
54
+ end
55
+
56
+ def identifier_symbol
57
+ if options['identifier']
58
+ options['identifier'] == "identifier" ? :id : options['identifier']
59
+ end
60
+ end
61
+
62
+ def fields
63
+ fs = if options["detect_fields"]
64
+ Array.new(options["fields"]).concat(introspected_fields)
65
+ else
66
+ options["fields"]
67
+ end
68
+ fs.reject {|f| f.blank? }.uniq
69
+ end
70
+
71
+ def introspected_fields
72
+ class_name.constantize.columns_hash.keys
73
+ end
74
+
75
+ # split at wrap_at chars, two indentations
76
+ def formatted_fields
77
+ two_indents = indent * 2
78
+ fields_string = fields.reduce([]) do |memo, f|
79
+ if !memo.last.nil?
80
+ now = "#{memo.last} :#{f},"
81
+ if now.length > options["wrap_at"].to_i
82
+ memo << ":#{f},"
83
+ else
84
+ memo[memo.length - 1] = now
85
+ end
86
+ else
87
+ memo << " :#{f},"
88
+ end
89
+ memo
90
+ end.join("\n#{two_indents}")
91
+
92
+ fields_string[0,fields_string.length - 1]
93
+ end
94
+
95
+ def associations
96
+ as = if options["detect_associations"]
97
+ Array.new(options["associations"]).concat(introspected_associations.keys)
98
+ else
99
+ options["associations"]
100
+ end
101
+ as.reject {|f| f.blank? }.uniq
102
+ end
103
+
104
+ def introspected_associations
105
+ class_name.constantize.reflections
106
+ end
107
+
108
+ def association_blueprint(association_name)
109
+ ", blueprint: #{association_class(association_name)}"
110
+ end
111
+
112
+ def association_class(association_name)
113
+ introspected_name = if introspected_associations[association_name].respond_to?(:klass)
114
+ introspected_associations[association_name].klass.to_s
115
+ else
116
+ nil
117
+ end
118
+ "#{introspected_name || association_name.camelcase}Blueprint"
119
+ end
120
+
121
+ def indent
122
+ user_intended = {two: " ", four: " ", tab:"\t"}[options["indentation"].intern]
123
+ user_intended.nil? ? " " : user_intended
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,14 @@
1
+ class <%= class_name %>Blueprint < Blueprinter::Base
2
+ <% if identifier_symbol -%>
3
+ <%= indent -%>identifier :<%= identifier_symbol %>
4
+
5
+ <% end -%>
6
+ <% if fields.any? -%>
7
+ <%= indent -%>fields<%= formatted_fields %>
8
+
9
+ <% end -%>
10
+ <% associations.each do |a| -%>
11
+ <%= indent -%>association :<%= a -%><%= association_blueprint(a) %>
12
+
13
+ <% end -%>
14
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blueprinter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.21.0
4
+ version: 0.22.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Hess
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-12-19 00:00:00.000000000 Z
12
+ date: 2019-12-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: factory_bot
@@ -151,6 +151,20 @@ dependencies:
151
151
  - - "~>"
152
152
  - !ruby/object:Gem::Version
153
153
  version: 0.9.11
154
+ - !ruby/object:Gem::Dependency
155
+ name: ammeter
156
+ requirement: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - "~>"
159
+ - !ruby/object:Gem::Version
160
+ version: 1.1.4
161
+ type: :development
162
+ prerelease: false
163
+ version_requirements: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - "~>"
166
+ - !ruby/object:Gem::Version
167
+ version: 1.1.4
154
168
  description: Blueprinter is a JSON Object Presenter for Ruby that takes business objects
155
169
  and breaks them down into simple hashes and serializes them to JSON. It can be used
156
170
  in Rails in place of other serializers (like JBuilder or ActiveModelSerializers).
@@ -184,6 +198,8 @@ files:
184
198
  - lib/blueprinter/version.rb
185
199
  - lib/blueprinter/view.rb
186
200
  - lib/blueprinter/view_collection.rb
201
+ - lib/generators/blueprinter/blueprint_generator.rb
202
+ - lib/generators/blueprinter/templates/blueprint.rb
187
203
  - lib/tasks/blueprinter_tasks.rake
188
204
  homepage: https://github.com/procore/blueprinter
189
205
  licenses: