# shopapp_rails_new.rb # Rails template for shoplift app # echo "Creating shoplift app $1" # ~/.rvm/scripts/rvm use 2.5.1 # rails new $1 --database=postgresql --skip-turbolinks --skip-test-unit # --skip-bundle --skip-test-unit --template=shopapp_rails_new.rb # gem file remove_file "Gemfile" add_file "Gemfile" add_source 'https://rubygems.org' append_to_file 'Gemfile', "ruby '2.5.1'" gem 'rails', '~> 5.2' gem 'pg', '~> 1.0' gem 'puma' gem 'sass-rails' gem 'shopapp' gem 'uglifier', '>= 1.3.0' gem 'mini_racer' gem 'jbuilder', '~> 2.5' gem 'bootsnap', '>= 1.1.0', require: false gem "audited", "~> 4.7" gem_group :development do gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' end run 'bundle install' remove_file "config/database.yml" create_file "config/database.yml", <<~DATABASE default: &default adapter: postgresql encoding: unicode # For details on connection pooling, see Rails configuration guide # http://guides.rubyonrails.org/configuring.html#database-pooling pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> development: <<: *default database: #{@app_name}_development test: <<: *default database: #{@app_name}_test production: <<: *default database: #{@app_name}_production username: <%= ENV['#{@app_name.upcase}_DATABASE_USERNAME'] %> password: <%= ENV['#{@app_name.upcase}_DATABASE_PASSWORD'] %> DATABASE route 'shoplift_single_sign_on' generate(:model, "company", "code:string", "name:string", "info:json") generate(:model, "company_state", "code:string", "state:string") generate('audited:install') insert_into_file "app/models/company.rb", " audited\n", :after => "ApplicationRecord\n" insert_into_file "app/models/company_state.rb", " audited\n", :after => "ApplicationRecord\n" rails_command "db:drop", env: 'development' rails_command "db:create", env: 'development' rails_command "db:migrate", env: 'development' remove_file "app/assets/stylesheets/application.css" create_file "app/assets/stylesheets/application.scss", <<~SCSS /* *= require_self * * Insert what you need after require_self, such as "require rails_bootstrap_forms". */ @import "shopapp"; SCSS remove_file "app/assets/javascript/application.js" create_file "app/assets/javascript/application.js", <<~JS //= require shopapp //= require_tree . JS remove_file "app/views/layouts/application.html.erb" create_file "app/views/layouts/application.html.haml", <<~LAYOUT !!! %html %head %meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/ %title #{@app_name} = csrf_meta_tags = stylesheet_link_tag 'application', media: 'all' = javascript_include_tag 'application' %body = render 'shopapp/shopapp' do = yield LAYOUT create_file '.gitignore', <<~GITIGNORE # Ignore bundler config. /.bundle # Ignore all logfiles and tempfiles. /log/* /tmp/* !/log/.keep !/tmp/.keep /node_modules /yarn-error.log .byebug_history /import # this is so we can develop shopapp gem inside this project /vendor/shopapp /config/local.yml /public/assets GITIGNORE @app_dev_port = ENV['dev_port'] || 3999 create_file 'config/settings.yml', <<~SETTINGS # Authlift setup port: #{@app_dev_port} # << TODO: CHANGE THIS AND SAME BELLOW IN authlift_redirect_uri shopapp_name: #{@app_name} authlift_url: <%= ((Rails.env == 'development') ? 'http://localhost:3031/' : 'https://accounts.shoplift.fi/') %> authlift_app_id: #{@app_id = SecureRandom.hex 32} authlift_app_secret: #{@app_secret = SecureRandom.hex 32} authlift_redirect_uri: http://localhost:#{@app_dev_port}/auth/ authlift_default_app_key: authlift_default_scope: # Override secret key base in local.yml if you do not want to use env. variables # secret_key_base: ## SecureRandom.hex 64 SETTINGS create_file 'config/local.yml', <<~SETTINGS authlift_url: 'https://accounts.shoplift.fi/' SETTINGS create_file 'lib/templates/rails/scaffold_controller/controller.rb.tt', <<~TEMPLATE <% if namespaced? -%> require_dependency "<%= namespaced_path %>/application_controller" <% end -%> <% module_namespacing do -%> class <%= controller_class_name %>Controller < UserAuthenticatedController before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy] # GET <%= route_url %> def index @<%= plural_table_name %> = <%= orm_class.all(class_name) %> end # GET <%= route_url %>/1 def show end # GET <%= route_url %>/new def new @<%= singular_table_name %> = <%= orm_class.build(class_name) %> end # GET <%= route_url %>/1/edit def edit end # POST <%= route_url %> def create @<%= singular_table_name %> = <%= orm_class.build(class_name, "\#{singular_table_name}_params") %> if @<%= orm_instance.save %> redirect_to @<%= singular_table_name %>, notice: <%= "'\#{human_name} was successfully created.'" %> else render :new end end # PATCH/PUT <%= route_url %>/1 def update if @<%= orm_instance.update("\#{singular_table_name}_params") %> redirect_to @<%= singular_table_name %>, notice: <%= "'\#{human_name} was successfully updated.'" %> else render :edit end end # DELETE <%= route_url %>/1 def destroy @<%= orm_instance.destroy %> redirect_to <%= index_helper %>_url, notice: <%= "'\#{human_name} was successfully destroyed.'" %> end private # Use callbacks to share common setup or constraints between actions. def set_<%= singular_table_name %> @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %> end # Only allow a trusted parameter "white list" through. def <%= "\#{singular_table_name}_params" %> <%- if attributes_names.empty? -%> params.fetch(:<%= singular_table_name %>, {}) <%- else -%> params.require(:<%= singular_table_name %>).permit(<%= attributes_names.map { |name| ":\#{name}" }.join(', ') %>) <%- end -%> end end <% end -%> TEMPLATE git :init git add: '.' git commit: "-a -m 'shopapp created'" unless ENV['skip_github_remote'] github_organization = ENV['github_organization'] || 'zwr' github_project_name = ENV['github_project_name'] || @app_name git remote: "add origin git@github.com:#{github_organization}/#{github_project_name}.git" git push: '-u origin master' unless ENV['skip_github_push'] end puts "Shopapp #{@app_name} created!" puts puts "To activate app in Authlift, execute the following:" puts " app = Doorkeeper::Application.find_or_create_by(name: '#{@app_name}')" puts " app.update_attributes uid: '#{@app_id}'," puts " secret: '#{@app_secret}'," puts " redirect_uri: 'http://localhost:#{@app_dev_port}/auth/'" puts " Company.find_by_code(:zwr).applications << app unless Company.find_by_code(:zwr).applications.include? app" puts puts "To see the power of Shopapp, do this (on Mac):" puts puts " $ cd #{@app_name}" puts " $ rails g scaffold dog name:string" puts " $ rake db:migrate" puts " $ open http://localhost:3999/dogs ; rails s"