adhoc-generators 0.0.3 → 0.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 079b8f53f307e99e274f7b852b21ac5e5d354ff1
4
- data.tar.gz: 18e9ef14a4ef66cb7af79cee03db69946795413b
3
+ metadata.gz: 754919abd00031ded4698235eff5f7cf5d323deb
4
+ data.tar.gz: 42a455e5fad7083b936b91328fe539d3cc853f8e
5
5
  SHA512:
6
- metadata.gz: 184c3bbfca90659bdcbc345e36a84142736edbc50ad87438c19819d9acf3c246942fa9cf848db47f42528b7f154578fe2647a7a12a1ad96d452d7e4e23568ed4
7
- data.tar.gz: 355ebf3d01a900c03b59f4ffb45a64e05a5411a40c75cac26b855bd387a23c49afb4be7d5506fd390f64a394e80ef97cb9b874d753a3d38ee3cb370f2a0cf6ac
6
+ metadata.gz: ff077bf96bde8106723f0f9dfc601de959b7cd26da5d6ddceb019003f08d23cf8fbbf05d819c81eb8a9b2bafd1f9c5e0b759f784b3f8dc44368ad93dfdca8a4b
7
+ data.tar.gz: bad0b54aef752a882012e7e8a5935f7000df73cabddf9e09fe3a969073e7f07d98e2877f745975e850431eda3aa37e8b5733bc2db4d84addb3565f2ee4865c26
@@ -6,7 +6,7 @@ module Adhoc
6
6
  module Generators
7
7
  class PortfolioGenerator < Base
8
8
  include Rails::Generators::Migration
9
- no_tasks { attr_accessor :name }
9
+ no_tasks { attr_accessor :name, :attributes }
10
10
 
11
11
  argument :name, type: :string, default: 'Portfolio', banner: 'PortfolioBanner'
12
12
 
@@ -18,8 +18,10 @@ module Adhoc
18
18
  super
19
19
  print_usage unless name.underscore =~ /^[a-z][a-z0-9_\/]+$/
20
20
  @namespace_model = options.namespace_model?
21
+
22
+ @attributes = []
21
23
 
22
- %w[title:string summary:string description:string service_id:integer].each do |arg|
24
+ ['title:string', 'summary:string', 'description:string', 'service_id:integer'].each do |arg|
23
25
  @attributes << Rails::Generators::GeneratedAttribute.new(*arg.split(':'))
24
26
  end
25
27
 
@@ -40,7 +42,7 @@ module Adhoc
40
42
  template 'controller.rb', "app/controllers/#{plural_name}_controller.rb"
41
43
  template 'helper.rb', "app/helpers/#{plural_name}_helper.rb"
42
44
 
43
- controller_actions.each do |action|
45
+ %w[index show new edit].each do |action|
44
46
  template "views/#{action}.html.erb", "app/views/#{plural_name}/#{action}.html.erb"
45
47
  end
46
48
 
@@ -70,6 +72,10 @@ module Adhoc
70
72
  file_name.capitalize
71
73
  end
72
74
 
75
+ def plural_class_name
76
+ plural_name.camelize
77
+ end
78
+
73
79
  def plural_name
74
80
  file_name.pluralize
75
81
  end
@@ -89,7 +95,7 @@ module Adhoc
89
95
  end
90
96
 
91
97
  def controller_actions
92
- %w[index show new create edit update destroy]
98
+ %w[index show new edit create update destroy]
93
99
  end
94
100
 
95
101
  def controller_methods(dir_name)
@@ -98,6 +104,22 @@ module Adhoc
98
104
  end.join("\n").strip
99
105
  end
100
106
 
107
+ def read_template(relative_path)
108
+ ERB.new(File.read(find_in_source_paths(relative_path)), nil, '-').result(binding)
109
+ end
110
+
111
+ def action?(name)
112
+ controller_actions.include? name.to_s
113
+ end
114
+
115
+ def actions?(*names)
116
+ names.all? { |name| action? name }
117
+ end
118
+
119
+ def singular_name
120
+ name.underscore
121
+ end
122
+
101
123
  def item_resource
102
124
  name.underscore.gsub('/','_')
103
125
  end
@@ -118,7 +140,7 @@ module Adhoc
118
140
  elsif options[:action].to_s == "edit"
119
141
  "edit_#{item_resource}_#{suffix}(#{name})"
120
142
  else
121
- if name.include?('::') && !@namespace_model
143
+ if name.include?('::')
122
144
  namespace = singular_name.split('/')[0..-2]
123
145
  "[:#{namespace.join(', :')}, #{name}]"
124
146
  else
@@ -143,6 +165,18 @@ module Adhoc
143
165
  end
144
166
  end
145
167
 
168
+ def form_partial?
169
+ actions? :new, :edit
170
+ end
171
+
172
+ def render_form
173
+ if form_partial?
174
+ "<%= render \"form\" %>"
175
+ else
176
+ read_template("views/_form.html.rb")
177
+ end
178
+ end
179
+
146
180
  # FIXME: Should be proxied to ActiveRecord::Generators::Base
147
181
  # Implement the required interface for Rails::Generators::Migration.
148
182
  def self.next_migration_number(dirname) #:nodoc:
@@ -1,7 +1,7 @@
1
1
  def create
2
- @<%= instance_name %> = <%= class_name %>.new(params[:<%= instance_name %>])
2
+ @<%= instance_name %> = <%= model_name %>.new(params[:<%= instance_name %>])
3
3
  if @<%= instance_name %>.save
4
- redirect_to <%= item_url %>, :notice => "Successfully created <%= class_name.underscore.humanize.downcase %>."
4
+ redirect_to <%= item_url %>, :notice => "Successfully created <%= model_name.underscore.humanize.downcase %>."
5
5
  else
6
6
  render :new
7
7
  end
@@ -1,5 +1,5 @@
1
1
  def destroy
2
- @<%= instance_name %> = <%= class_name %>.find(params[:id])
2
+ @<%= instance_name %> = <%= model_name %>.find(params[:id])
3
3
  @<%= instance_name %>.destroy
4
- redirect_to <%= items_url %>, :notice => "Successfully destroyed <%= class_name.underscore.humanize.downcase %>."
4
+ redirect_to <%= items_url %>, :notice => "Successfully destroyed <%= model_name.underscore.humanize.downcase %>."
5
5
  end
@@ -1,3 +1,3 @@
1
1
  def edit
2
- @<%= instance_name %> = <%= class_name %>.find(params[:id])
2
+ @<%= instance_name %> = <%= model_name %>.find(params[:id])
3
3
  end
@@ -1,3 +1,3 @@
1
1
  def index
2
- @<%= instances_name %> = <%= class_name %>.all
2
+ @<%= instances_name %> = <%= model_name %>.all
3
3
  end
@@ -1,3 +1,3 @@
1
1
  def new
2
- @<%= instance_name %> = <%= class_name %>.new
2
+ @<%= instance_name %> = <%= model_name %>.new
3
3
  end
@@ -1,3 +1,3 @@
1
1
  def show
2
- @<%= instance_name %> = <%= class_name %>.find(params[:id])
2
+ @<%= instance_name %> = <%= model_name %>.find(params[:id])
3
3
  end
@@ -1,7 +1,7 @@
1
1
  def update
2
- @<%= instance_name %> = <%= class_name %>.find(params[:id])
2
+ @<%= instance_name %> = <%= model_name %>.find(params[:id])
3
3
  if @<%= instance_name %>.update_attributes(params[:<%= instance_name %>])
4
- redirect_to <%= item_url %>, :notice => "Successfully updated <%= class_name.underscore.humanize.downcase %>."
4
+ redirect_to <%= item_url %>, :notice => "Successfully updated <%= model_name.underscore.humanize.downcase %>."
5
5
  else
6
6
  render :edit
7
7
  end
@@ -1,4 +1,4 @@
1
- class Create<%= class_name.pluralize.delete('::') %> < ActiveRecord::Migration
1
+ class Create<%= model_name.pluralize.delete('::') %> < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :<%= table_name || plural_name.split('/').last %> do |t|
4
4
  <%- for attribute in attributes -%>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adhoc-generators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - folklore