<commit_before># this generator based on rails_admin's install generator.
# https://www.github.com/sferik/rails_admin/master/lib/generators/rails_admin/install_generator.rb

require 'rails/generators'

# http://guides.rubyonrails.org/generators.html
# http://rdoc.info/github/wycats/thor/master/Thor/Actions.html

module Koudoku
  class InstallGenerator < Rails::Generators::Base

    # Not sure what this does.
    source_root "#{Koudoku::Engine.root}/app/views/koudoku/subscriptions"

    include Rails::Generators::Migration

    argument :subscription_owner_model, :type => :string, :required => true, :desc => "Owner of the subscription"
    desc "Koudoku installation generator"

    def install
      
      unless defined?(Koudoku)
        gem("koudoku")
      end
      
      require "securerandom"
      api_key = SecureRandom.uuid
      create_file 'config/initializers/koudoku.rb' do
      <<-RUBY
Koudoku.setup do |config|
  config.webhooks_api_key = "#{api_key}"
  config.subscriptions_owned_by = :#{subscription_owner_model}
  config.stripe_publishable_key = ENV['STRIPE_PUBLISHABLE_KEY']
  config.stripe_secret_key = ENV['STRIPE_SECRET_KEY']
  # config.prorate = false # Default is true, set to false to disable prorating subscriptions
  # config.free_trial_length = 30
end
RUBY
      end

      # Generate subscription.
      generate("model", "subscription stripe_id:string plan_id:integer last_four:string coupon_id:integer card_type:string current_price:float #{subscription_owner_model}_id:integer")
      gsub_file "app/models/subscription.rb", /ActiveRecord::Base/, "ActiveRecord::Base\n  include Koudoku::Subscription\n\n  belongs_to :#{subscription_owner_model}\n  belongs_to :coupon\n"
	  inject_into_file("app/models/subscription.rb", "  attr_accessible :credit_card_token\n", :before => 'end') if Rails::VERSION::STRING.start_with? '3'
      
      # Add the plans.
      generate("model", "plan name:string stripe_id:string price:float interval:string features:text highlight:boolean display_order:integer")
      gsub_file "app/models/plan.rb", /ActiveRecord::Base/, "ActiveRecord::Base\n  include Koudoku::Plan\n  belongs_to :#{subscription_owner_model}\n  belongs_to :coupon\n  has_many :subscriptions\n"

      # Add coupons.
      generate("model coupon code:string free_trial_length:string")
      gsub_file "app/models/coupon.rb", /ActiveRecord::Base/, "ActiveRecord::Base\n  has_many :subscriptions\n"
      
      # Update the owner relationship.
      gsub_file "app/models/#{subscription_owner_model}.rb", /ActiveRecord::Base/, "ActiveRecord::Base\n\n  # Added by Koudoku.\n  has_one :subscription\n\n"
      
      # Install the pricing table.
      ["_social_proof.html.erb"].each do |file|
        copy_file file, "app/views/koudoku/subscriptions/#{file}"
      end

      # Add webhooks to the route.
      gsub_file "config/routes.rb", /application.routes.draw do/,  <<-RUBY
application.routes.draw do

  # Added by Koudoku.
  mount Koudoku::Engine, at: 'koudoku'
  scope module: 'koudoku' do
    get 'pricing' => 'subscriptions#index', as: 'pricing'
  end<commit_msg>-refactor: using the route() command instead of a fragile gsub_file() to add routes in install_generator<commit_after># this generator based on rails_admin's install generator.
# https://www.github.com/sferik/rails_admin/master/lib/generators/rails_admin/install_generator.rb

require 'rails/generators'

# http://guides.rubyonrails.org/generators.html
# http://rdoc.info/github/wycats/thor/master/Thor/Actions.html

module Koudoku
  class InstallGenerator < Rails::Generators::Base

    # Not sure what this does.
    source_root "#{Koudoku::Engine.root}/app/views/koudoku/subscriptions"

    include Rails::Generators::Migration

    argument :subscription_owner_model, :type => :string, :required => true, :desc => "Owner of the subscription"
    desc "Koudoku installation generator"

    def install
      
      unless defined?(Koudoku)
        gem("koudoku")
      end
      
      require "securerandom"
      api_key = SecureRandom.uuid
      create_file 'config/initializers/koudoku.rb' do
      <<-RUBY
Koudoku.setup do |config|
  config.webhooks_api_key = "#{api_key}"
  config.subscriptions_owned_by = :#{subscription_owner_model}
  config.stripe_publishable_key = ENV['STRIPE_PUBLISHABLE_KEY']
  config.stripe_secret_key = ENV['STRIPE_SECRET_KEY']
  # config.prorate = false # Default is true, set to false to disable prorating subscriptions
  # config.free_trial_length = 30
end
RUBY
      end

      # Generate subscription.
      generate("model", "subscription stripe_id:string plan_id:integer last_four:string coupon_id:integer card_type:string current_price:float #{subscription_owner_model}_id:integer")
      gsub_file "app/models/subscription.rb", /ActiveRecord::Base/, "ActiveRecord::Base\n  include Koudoku::Subscription\n\n  belongs_to :#{subscription_owner_model}\n  belongs_to :coupon\n"
	  inject_into_file("app/models/subscription.rb", "  attr_accessible :credit_card_token\n", :before => 'end') if Rails::VERSION::STRING.start_with? '3'
      
      # Add the plans.
      generate("model", "plan name:string stripe_id:string price:float interval:string features:text highlight:boolean display_order:integer")
      gsub_file "app/models/plan.rb", /ActiveRecord::Base/, "ActiveRecord::Base\n  include Koudoku::Plan\n  belongs_to :#{subscription_owner_model}\n  belongs_to :coupon\n  has_many :subscriptions\n"

      # Add coupons.
      generate("model coupon code:string free_trial_length:string")
      gsub_file "app/models/coupon.rb", /ActiveRecord::Base/, "ActiveRecord::Base\n  has_many :subscriptions\n"
      
      # Update the owner relationship.
      gsub_file "app/models/#{subscription_owner_model}.rb", /ActiveRecord::Base/, "ActiveRecord::Base\n\n  # Added by Koudoku.\n  has_one :subscription\n\n"
      
      # Install the pricing table.
      ["_social_proof.html.erb"].each do |file|
        copy_file file, "app/views/koudoku/subscriptions/#{file}"
      end

      # Add webhooks to the route.
      
      route <<-RUBY

  # Added by Koudoku.
  mount Koudoku::Engine, at: 'koudoku'
  scope module: 'koudoku' do
    get 'pricing' => 'subscriptions#index', as: 'pricing'
  end
{'fraction_non_alphanumeric': 0.11723254324151185, 'fraction_numerical': 0.0009609224855861628, 'mean_word_length': 4.089649551752241, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3214904', 'n_tokens_mistral': 2040, 'n_tokens_neox': 2002, 'n_words': 412}