<commit_before>class Script < ActiveRecord::Base
  attr_accessible :name, :url, :body, :body_url,
                  :docker_host, :docker_container_id, :docker_status

  validates :user_id, :presence => true
  validates :name, :presence => true, :uniqueness => {:scope => :user_id}

  scope :valid, lambda { where("deleted = ?", false) }

  belongs_to :user
  has_many :runs, :class_name => 'ScriptRun'

  after_destroy :rethink_delete

  extend FriendlyId
  friendly_id :name, use: :slugged

  include Workflow
  workflow_column :docker_status

  workflow do
    state :stopped do
      event :start, :transitions_to => :running
    end
    state :running do
      event :stop, :transitions_to => :stopped
    end

  end

  include RethinkDB::Shortcuts

  def self.safe_create(params)
    script = Script.new
    #defaults
    script.deleted = false

    script.name = params[:name]
    script.save
    script
  end

  def safe_update(params)
    unless params[:url].blank?
      self.url = params[:url]
      # response = RestClient.get(params[:url])
    end
    unless params[:body].blank?
      r.table('scripts').get(script_name).update(body:params[:body]).run(R)
    end
    save
  end

  def rethink_insert
    r.table('scripts').insert(id:script_name,
                              key:UUID.generate,
                              inventory: {btc:1},
                              trades: []).run(R)
    r.table('storage').insert({_cointhink_id_:script_name}).run(R)
  end

  def rethink_delete
    r.table('scripts').get(script_name).delete.run(R)
    r.table('storage').get(script_name).delete.run(R)
    r.db('cointhink').
      table('signals').
      get_all(script_name, {index:'name'}).
      delete.
      run(R)
  end

  def reset!
    r.table('storage').get(script_name).update({}).run(R)
  end

  def script_name
    "#{user.username}/#{name}"
  end

  def doc<commit_msg>replace the right way<commit_after>class Script < ActiveRecord::Base
  attr_accessible :name, :url, :body, :body_url,
                  :docker_host, :docker_container_id, :docker_status

  validates :user_id, :presence => true
  validates :name, :presence => true, :uniqueness => {:scope => :user_id}

  scope :valid, lambda { where("deleted = ?", false) }

  belongs_to :user
  has_many :runs, :class_name => 'ScriptRun'

  after_destroy :rethink_delete

  extend FriendlyId
  friendly_id :name, use: :slugged

  include Workflow
  workflow_column :docker_status

  workflow do
    state :stopped do
      event :start, :transitions_to => :running
    end
    state :running do
      event :stop, :transitions_to => :stopped
    end

  end

  include RethinkDB::Shortcuts

  def self.safe_create(params)
    script = Script.new
    #defaults
    script.deleted = false

    script.name = params[:name]
    script.save
    script
  end

  def safe_update(params)
    unless params[:url].blank?
      self.url = params[:url]
      # response = RestClient.get(params[:url])
    end
    unless params[:body].blank?
      r.table('scripts').get(script_name).update(body:params[:body]).run(R)
    end
    save
  end

  def rethink_insert
    r.table('scripts').insert(id:script_name,
                              key:UUID.generate,
                              inventory: {btc:1},
                              trades: []).run(R)
    r.table('storage').insert({_cointhink_id_:script_name}).run(R)
  end

  def rethink_delete
    r.table('scripts').get(script_name).delete.run(R)
    r.table('storage').get(script_name).delete.run(R)
    r.db('cointhink').
      table('signals').
      get_all(script_name, {index:'name'}).
      delete.
      run(R)
  end

  def reset!
    r.table('storage').get(script_name).replace({'_cointhink_id_'=>script_name}).run(R)
  end

  def script_name
    "#{user.username}/#{name}"
  end

  def doc
{'fraction_non_alphanumeric': 0.13141361256544504, 'fraction_numerical': 0.0005235602094240838, 'mean_word_length': 2.798210735586481, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4584428', 'n_tokens_mistral': 1286, 'n_tokens_neox': 1249, 'n_words': 250}