Skip to content

Instantly share code, notes, and snippets.

@dux
Last active August 29, 2015 13:57
Show Gist options
  • Save dux/9363510 to your computer and use it in GitHub Desktop.
Save dux/9363510 to your computer and use it in GitHub Desktop.
Simple native rails generate, without Rails and Rails generators
#!/usr/bin/ruby
require 'colorize'
@locations = []
def generate_file( file_name, data )
if File.exist?(file_name)
info = 'exists'.red
else
@locations.push file_name
info = 'create'.green
File.open(file_name, 'w') { |file| file.write(data) }
end
print info.rjust(25)
print ": #{file_name}\n"
end
file_name = ARGV[0]
unless file_name
print "Usage: generate user_link\n"
exit
end
class_name = ARGV[0].gsub(/_(\w)/){ "#{$1.capitalize}" }
class_name[0,1] = class_name[0,1].capitalize
if ARGV[1]
file_names = ARGV[1]
class_names = ARGV[1].gsub(/_(\w)/){ "#{$1.capitalize}" }
class_names[0,1] = class_name[0,1].capitalize
else
file_names = file_name+'s'
class_names = class_name+'s'
end
print "sin: #{file_name}\nplu: #{file_names}\nok (y)es and (m)igration, 'ym' for all: "
res = STDIN.gets.chomp
generate_migration = res =~ /m/
unless res =~ /^y/i
print "(y)es not detected, exiting.\n"
exit
end
if file_names =~ /ss/ && ! ['news'].index(file_name)
p "Koristi singular!"
exit
end
file_data = {}
Dir.mkdir("app/views/admin") rescue 0
Dir.mkdir("app/views/admin/#{file_names}") rescue 0
generate_file "app/models/#{file_name}.rb", %[class #{class_name} < MasterModel
#validates :name, presence:{ message:'Name is required' }
end]
generate_file "app/controllers/#{file_names}_controller.rb", %[class #{class_names}Controller < ApplicationController
def show
@#{file_name} = #{class_name}.find params[:id].to_i
end
def index
@#{file_names} = #{class_name}.all.page params[:page]
end
end]
generate_file "app/api/#{file_name}_api.rb", %[class #{class_name}Api < MasterApi
end]
if generate_migration
generate_file "db/migrate/#{Time.now.strftime("%Y%m%d%H%M%S")}_create_#{file_name}.rb", %[class Create#{class_name} < ActiveRecord::Migration
def change
create_table :#{file_names} do |t|
t.string :name
t.integer :created_by
t.integer :updated_by
t.datetime :created_at
t.datetime :updated_at
end
end
end]
else
print "Skipping migration generation (no 'g' provided)\n"
end
generate_file "app/views/admin/#{file_names}/_wrap.haml", %[- @base_path = @#{file_name}.path(:admin)
= link_to 'Show', "/r/#]+%[{@#{file_name}.id}", :style=>'float:right;', :class=>'no-pjax'
%h1= @#{file_name}.name
%br
= bs_tabs [[@base_path,'#{class_name}']]
%br
= yield
]
generate_file "app/views/admin/#{file_names}/show.haml", %[= render :layout=>'/admin/#{file_names}/wrap' do
.form
= bs_form @#{file_name}, :horizontal=>true, :done=>'refresh' do
= bs_input :name
= bs_submit 'Save', '/admin/#{file_names}', 'go back to list'
]
generate_file "app/views/admin/#{file_names}/index.haml", %[%h1
#{class_name}
%button.btn{ :onclick=>%[$(this).hide().parent().next().show();] } +
.alert.alert-info.none
= bs_form #{class_name}.new, :horizontal=>true, :done=>'refresh' do
= bs_input :name
= bs_submit 'Create new #{class_name}', %[$(this).closest('.alert').hide().prev().find('button').show();]
%table.table.table-striped.hover
%tr
%th{ :style=>'width:40px;' } ID
%th Name
%th{ :style=>'width:120px;' } Created at
- for el in (@rows = #{class_name}.desc.page(params[:page]))
%tr{ :href=>el.path(:admin) }
%td= el.id
%td
= el.name
%td= el.created_at.short
= will_paginate @rows
]
system @locations.map{ |el| "subl #{el}" }.join(' && ')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment