<commit_before>class Lolita::RestController < ApplicationController
  include Lolita::Controllers::UserHelpers
  include Lolita::Controllers::InternalHelpers
  
  before_filter :authenticate_lolita_user!
  layout "lolita/application"
  
  def new
    build_resource
    show_form
  end

  def create
    build_resource
    save_and_redirect
  end

  def edit
    get_resource
    show_form
  end
  
  def update
    get_resource
    if self.resource
      self.resource=resource_with_attributes(self.resource,resource_attributes)
      save_and_redirect
    end
  end

  def destroy
    get_resource
    if self.resource && self.resource.destroy
      flash.now[:notice] = I18n.t "lolita.shared.destroy_notice"
    else
      flash.now[:alert] = I18n.t "lolita.shared.destroy_alert"
    end
    redirect_to :action=>"index"
  end

  def index
    respond_to do |format|
      format.html do
        build_response_for(:list,:page=>page)
      end
      format.json do
        render :json=>page
      end
    end
  end

  private

  def show_form
    #TODO Valdis: ja es extendoju rest_controller un gribu pārdefinēt edit actionu, man vajag lai
    #  varu norādīt citu šablonu, piemēram, manā gadījumā ir services_controller un jamais meklē
    #  "services/form", bet es gribu izmantot to pašu "lolita/rest/form"
    #  tagad man viss šitais jāpārkopē uz projektu

    build_response_for(:tabs)
    if request.xhr?
      render :form, :layout => false
    else
      render :form
    end
  end
  
  def save_and_redirect
      respond_to do |format|
        format.html do
          if self.resource.save
            flash.now[:notice] = I18n.t "lolita.shared.save_notice"
          else
            flash.now[:alert] = I18n.t "lolita.shared.save_alert"
          end
          show_form
        end
        format.json do
          render :status => self.resource.save ? 200 : 400, :json => self.resource
        end
      end
  end
  
  def to_list<commit_msg>Fixed bug in REST, reload object after save<commit_after>class Lolita::RestController < ApplicationController
  include Lolita::Controllers::UserHelpers
  include Lolita::Controllers::InternalHelpers
  
  before_filter :authenticate_lolita_user!
  layout "lolita/application"
  
  def new
    build_resource
    show_form
  end

  def create
    build_resource
    save_and_redirect
  end

  def edit
    get_resource
    show_form
  end
  
  def update
    get_resource
    if self.resource
      self.resource=resource_with_attributes(self.resource,resource_attributes)
      save_and_redirect
    end
  end

  def destroy
    get_resource
    if self.resource && self.resource.destroy
      flash.now[:notice] = I18n.t "lolita.shared.destroy_notice"
    else
      flash.now[:alert] = I18n.t "lolita.shared.destroy_alert"
    end
    redirect_to :action=>"index"
  end

  def index
    respond_to do |format|
      format.html do
        build_response_for(:list,:page=>page)
      end
      format.json do
        render :json=>page
      end
    end
  end

  private

  def show_form
    #TODO Valdis: ja es extendoju rest_controller un gribu pārdefinēt edit actionu, man vajag lai
    #  varu norādīt citu šablonu, piemēram, manā gadījumā ir services_controller un jamais meklē
    #  "services/form", bet es gribu izmantot to pašu "lolita/rest/form"
    #  tagad man viss šitais jāpārkopē uz projektu

    build_response_for(:tabs)
    if request.xhr?
      render :form, :layout => false
    else
      render :form
    end
  end
  
  def save_and_redirect
    respond_to do |format|
      format.html do
        if self.resource.save
          self.resource.reload
          flash.now[:notice] = I18n.t "lolita.shared.save_notice"
        else
          flash.now[:alert] = I18n.t "lolita.shared.save_alert"
        end
        show_form
      end
      format.json do
        render :status => self.resource.save ? 200 : 400, :json => self.resource.reload
      end
    end
  end
  
  def to_list
{'fraction_non_alphanumeric': 0.0690176322418136, 'fraction_numerical': 0.007052896725440806, 'mean_word_length': 2.5266429840142095, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '3618106', 'n_tokens_mistral': 1344, 'n_tokens_neox': 1315, 'n_words': 321}