Module: Tennpipes::Generators::Components::Actions

Included in:
Tennpipes::Generators::Component, Tennpipes::Generators::Controller, Helper, Mailer, Migration, Model, Project, Task
Defined in:
lib/tennpipes-init/generators/components/actions.rb

Instance Method Summary collapse

Instance Method Details

#controller_actions(fields) ⇒ Object

Takes in fields for routes in the form of get:index post:test delete:yada.

Examples:

controller_actions("get:index", "post:test")

Parameters:

  • fields (Array<String>)

    Array of controller actions and route name.



189
190
191
192
193
194
195
# File 'lib/tennpipes-init/generators/components/actions.rb', line 189

def controller_actions(fields)
  field_tuples = fields.map { |value| value.split(":") }
  action_declarations = field_tuples.map do |request, name|
    "#{request} :#{name} do\n\nend\n"
  end
  action_declarations.join("\n").gsub(/^/, " " * 2).gsub(/^\s*$/, "")
end

#create_helper_files(app, name) ⇒ Object



197
198
199
200
201
202
203
204
# File 'lib/tennpipes-init/generators/components/actions.rb', line 197

def create_helper_files(app, name)
  @helper_name  = "#{name.to_s.underscore.camelize}Helper"
  template 'templates/helper.rb.tt', destination_root(app, 'helpers', "#{name.to_s.underscore}_helper.rb")
  if test?
    include_component_module_for(:test)
    generate_helper_test(@helper_name, @project_name, @app_name)
  end
end

#current_migration_numberObject

Returns timestamp instead if :migration_format: in .components is “timestamp”



100
101
102
103
104
105
106
# File 'lib/tennpipes-init/generators/components/actions.rb', line 100

def current_migration_number
  if fetch_component_choice(:migration_format).to_s == 'timestamp'
    Time.now.utc.strftime("%Y%m%d%H%M%S")
  else
    return_last_migration_number + 1
  end.to_s
end

#indent_spaces(count) ⇒ Object

Returns space characters of given count.

Examples:

indent_spaces(2)


176
177
178
# File 'lib/tennpipes-init/generators/components/actions.rb', line 176

def indent_spaces(count)
  ' ' * count
end

#insert_mocking_include(library_name, options = {}) ⇒ Object

Injects the mock library include into the test class in test_config for setting up mock gen

Examples:

insert_mocking_include('Mocha::API'):
=> inject_into_file("test/test_config.rb", "  include Mocha::API\n", :after => /class.*?\n/)

Parameters:

  • library_name (String)

    Name of mocking library.

  • options (Hash) (defaults to: {})


163
164
165
166
167
168
# File 'lib/tennpipes-init/generators/components/actions.rb', line 163

def insert_mocking_include(library_name, options={})
  options.reverse_merge!(:indent => 2, :after => /class.*?\n/, :path => "test/test_config.rb")
  return unless File.exist?(destination_root(options[:path]))
  include_text = indent_spaces(2) + "include #{library_name}\n"
  inject_into_file(options[:path], include_text, :after => options[:after])
end

#insert_test_suite_setup(suite_text, options = {}) ⇒ Object

Injects the test class text into the test_config file for setting up the test gen.

Examples:

insert_test_suite_setup('...CLASS_NAME...')
=> inject_into_file("test/test_config.rb", TEST.gsub(/CLASS_NAME/, @app_name), :after => "set :environment, :test")

Parameters:

  • suite_text (String)

    Class name for test suite.

  • options (Hash) (defaults to: {})

    Additional options to pass into injection.



146
147
148
149
# File 'lib/tennpipes-init/generators/components/actions.rb', line 146

def insert_test_suite_setup(suite_text, options={})
  options.reverse_merge!(:path => "test/test_config.rb")
  create_file(options[:path], suite_text.gsub(/CLASS_NAME/, "#{@project_name}::#{@app_name}"))
end

#migration_exist?(filename) ⇒ Boolean

Return true if the migration already exist.

Parameters:

  • filename (String)

    File name of the migration file.

Returns:

  • (Boolean)


114
115
116
# File 'lib/tennpipes-init/generators/components/actions.rb', line 114

def migration_exist?(filename)
  Dir[destination_root("db/migrate/*_#{filename.underscore}.rb")].size > 0
end

#output_migration_file(filename, name, columns, options = {}) ⇒ Object

Generates a standalone migration file based on the given options and columns.

Examples:

output_migration_file(migration_name, name, columns,
  :base => AR_MIGRATION, :change_format => AR_CHANGE_MG,
  :add => Proc.new { |field, kind| "t.#{kind.underscore.gsub(/_/, '')} :#{field}" },
  :remove => Proc.new { |field, kind| "t.remove :#{field}" }
)

Parameters:

  • filename (String)

    File name of model migration.

  • name (String)

    Name of model.

  • columns (Array<String>)

    Array of column names and property type.

  • options (Hash) (defaults to: {})

    Additional migration options, e.g

    { :base "...text...", :change_format => "...text...",
      :add => proc { |field, kind| "add_column :#{table_name}, :#{field}, :#{kind}" },
      :remove => proc { |field, kind| "remove_column :#{table_name}, :#{field}" }
    


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/tennpipes-init/generators/components/actions.rb', line 64

def output_migration_file(filename, name, columns, options={})
  if behavior == :revoke
    remove_migration(name)
  else
    return if migration_exist?(filename)
    change_format = options[:change_format]
    migration_scan = filename.underscore.camelize.scan(/(Add|Remove).*?(?:(?:To|From).+?)*(?:To|From)((?:To|From)?.*?)$/).flatten
    direction, table_name = migration_scan[0].downcase, migration_scan[1].downcase.pluralize if migration_scan.any?
    tuples = direction ? columns.map { |value| value.split(":") } : []
    tuples.map! { |field, kind| kind =~ /datetime/i ? [field, 'DateTime'] : [field, kind] }
    add_columns    = tuples.map(&options[:add]).join("\n    ")
    remove_columns = tuples.map(&options[:remove]).join("\n    ")
    forward_text = change_format.gsub(/!TABLE!/, table_name).gsub(/!COLUMNS!/, add_columns) if tuples.any?
    back_text    = change_format.gsub(/!TABLE!/, table_name).gsub(/!COLUMNS!/, remove_columns) if tuples.any?
    contents = options[:base].dup.gsub(/\s{4}!UP!\n/m,   (direction == 'add' ? forward_text.to_s : back_text.to_s))
    contents.gsub!(/\s{4}!DOWN!\n/m, (direction == 'add' ? back_text.to_s : forward_text.to_s))
    contents = contents.gsub(/!FILENAME!/, filename.underscore).gsub(/!FILECLASS!/, filename.underscore.camelize)
    migration_number = current_migration_number
    contents.gsub!(/!VERSION!/, migration_number)
    migration_filename = "#{format("%03d", migration_number)}_#{filename.underscore}.rb"
    create_file(destination_root('db/migrate/', migration_filename), contents, :skip => true)
  end
end

#output_model_migration(filename, name, columns, options = {}) ⇒ Object

Generates the model migration file created when generating a new model.

Examples:

output_model_migration("AddPerson", "person", ["name:string", "age:integer"],
  :base => AR_MIGRATION,
  :column_format => Proc.new { |field, kind| "t.#{kind.underscore.gsub(/_/, '')} :#{field}" },
  :up => AR_MODEL_UP_MG, :down => AR_MODEL_DOWN_MG)

Parameters:

  • filename (String)

    File name of model migration.

  • name (String)

    Name of model.

  • columns (Array<String>)

    Array of column names and property type.

  • options (Hash) (defaults to: {})

    Additional migration options, e.g { :base => “.…text…”, :up => “..text…”,

    :down => "..text...", column_format => "t.column :#{field}, :#{kind}" }
    


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tennpipes-init/generators/components/actions.rb', line 24

def output_model_migration(filename, name, columns, options={})
  if behavior == :revoke
    remove_migration(filename)
  else
    return if migration_exist?(filename)
    model_name = name.to_s.pluralize
    field_tuples = columns.map { |value| value.split(":") }
    field_tuples.map! { |field, kind| kind =~ /datetime/i ? [field, 'DateTime'] : [field, kind] }
    column_declarations = field_tuples.map(&options[:column_format]).join("\n      ")
    contents = options[:base].dup.gsub(/\s{4}!UP!\n/m, options[:up]).gsub(/!DOWN!\n/m, options[:down])
    contents = contents.gsub(/!NAME!/, model_name.underscore.camelize).gsub(/!TABLE!/, model_name.underscore)
    contents = contents.gsub(/!FILENAME!/, filename.underscore).gsub(/!FILECLASS!/, filename.underscore.camelize)
    migration_number = current_migration_number
    contents = contents.gsub(/!FIELDS!/, column_declarations).gsub(/!VERSION!/, migration_number)
    migration_filename = "#{format("%03d", migration_number)}_#{filename.underscore}.rb"
    create_file(destination_root('db/migrate/', migration_filename), contents, :skip => true)
  end
end

#remove_migration(name) ⇒ Object

Removes the migration file based on the migration name.

Parameters:

  • name (String)

    File name of the migration.



124
125
126
127
128
129
130
131
132
# File 'lib/tennpipes-init/generators/components/actions.rb', line 124

def remove_migration(name)
  migration_path =  Dir[destination_root('db/migrate/*.rb')].find do |f|
    File.basename(f) =~ /#{name.to_s.underscore}/
  end
  return unless migration_path
  if behavior == :revoke
    create_file migration_path # we use create to reverse the operation of a revoke
  end
end

#return_last_migration_numberObject

Returns the number of the latest(most current) migration file.



91
92
93
94
95
# File 'lib/tennpipes-init/generators/components/actions.rb', line 91

def return_last_migration_number
  Dir[destination_root('db/migrate/*.rb')].map { |f|
    File.basename(f).match(/^(\d+)/)[0].to_i
  }.max.to_i || 0
end