<commit_before>class GroupsController < ApplicationController
  before_filter :login_required, :except => [:index, :show, :join]
  before_filter :check_permissions, :only => [:edit, :update, :close,
                                              :connect_group_to_twitter,
                                              :disconnect_twitter_group, :set_columns]
  before_filter :admin_required , :only => [:accept, :destroy]
  subtabs :index => [ [:most_active, [:activity_rate, Mongo::DESCENDING]], [:newest, [:created_at, Mongo::DESCENDING]],
                      [:oldest, [:created_at, Mongo::ASCENDING]], [:name, [:name, Mongo::ASCENDING]]]
  # GET /groups
  # GET /groups.json
  def index
    @state = "active"
    case params.fetch(:tab, "active")
      when "pendings"
        @state = "pending"
    end

    conds = {:state => @state, :private => false}

    if params[:q].blank?
      @groups = Group.where(conds).page(params["page"])
    else
      @groups = Group.filter(params[:q],conds)
    end

    respond_to do |format|
      format.html # index.html.haml
      format.json  { render :json => @groups }
      format.js do
        html = render_to_string(:partial => "group", :collection  => @groups)
        pagination = render_to_string(:partial => "shared/pagination", :object => @groups,
                                      :format => "html")
        render :json => {:html => html, :pagination => pagination }
      end
    end
  end

  # GET /groups/1
  # GET /groups/1.json
  def show
    @active_subtab = "about"

    if params[:id]
      @group = Group.find_by_slug_or_id(params[:id])
    else
      @group = current_group
    end
    raise Error404 if @group.nil?

    respond_to do |format|
      format.html # show.html.erb
      format.json  { render :json => @group }
    end
  end

  # GET /groups/new
  # GET /groups/new.json
  def new
    @group = Group.new

    respond_to do |format|
      format.html # new.html.erb
      format.json  { render :json => @group }
    end
  end

  # GET /groups/1/edit
  def edit
  end

  # POST /groups
  # POST /groups.json
  def create
    @group = Group.new
    @group.languages = params[:languages].split(',') if params[:languages]
    @group.safe_update(%w[name legend description default_tags subdomain logo forum enable_mathjax enable_latex
                          custom_favicon language theme signup_type custom_css wysiwyg_editor], params[:group])

    @group.safe_update(%w[isolate domain private], params[:group]) if current_user.admin?

    @group.owner = current_user
    @group.state = "active"

    respond_to do |format|
      if @group.save
        @group.create_default_widgets

        Jobs::Images.async.generate_group_thumbnails(@group.id)
        @group.add_member(current_user, "owner")
        flash[:notice] = I18n.t("groups.create.flash_notice")
        format.html { redirect_to(domain_url(:custom => @group.domain, :controller => "admin/manage", :action => "properties")) }
        format.json  { render :json => @group.to_json, :status => :created, :location => @group }
      else
        format.html { render :action => "new" }
        format.json { render :json => @group.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /groups/1
  # PUT /groups/1.json
  def update
    @group.languages = params[:languages].split(',') if params[:languages]
    @group.safe_update(%w[track_users name legend description default_tags subdomain logo logo_info forum enable_latex enable_mathjax
                          custom_favicon language languages current_theme_id reputation_rewards daily_cap reputation_constrains
                          has_adult_content registered_only enable_anonymous signup_type custom_css wysiwyg_editor layout
                          fb_button notification_opts auth_providers allow_any_openid], params[:group])
    @group.share.safe_update(%w[fb_app_id fb_secret_key fb_active starts_with ends_with enable_twitter twitter_user twitter_pattern], params[:group][:share]) if params[:group][:share]
    @group.safe_update(%w[isolate domain private has_custom_analytics has_custom_html has_custom_js], params[:group]) #if current_user.admin?
    @group.safe_update(%w[analytics_id analytics_vendor], params[:group]) if @group.has_custom_analytics
    @group.custom_html.update_attributes(params[:group][:custom_html] || {}) if @group.has_custom_html
    @group.notification_opts.safe_update(%w[questions_to_twitter badges_to_twitter favorites_to_twitter answers_to_twitter comments_to_twitter], params[:group][:notification_opts]) if params[:group][:notification_opts]

    if @group.domain == AppConfig.domain ||
        @group.domain.index(AppConfig.domain).nil? ||
        @group.user.role == 'admin'
      @group.has_custom_js = true
    else
      @group.has_custom_js = false
    end

    if params[:group][:logo]
      @group.logo_version += 1<commit_msg>fix bug that won't update groups when languages are empty<commit_after>class GroupsController < ApplicationController
  before_filter :login_required, :except => [:index, :show, :join]
  before_filter :check_permissions, :only => [:edit, :update, :close,
                                              :connect_group_to_twitter,
                                              :disconnect_twitter_group, :set_columns]
  before_filter :admin_required , :only => [:accept, :destroy]
  subtabs :index => [ [:most_active, [:activity_rate, Mongo::DESCENDING]], [:newest, [:created_at, Mongo::DESCENDING]],
                      [:oldest, [:created_at, Mongo::ASCENDING]], [:name, [:name, Mongo::ASCENDING]]]
  # GET /groups
  # GET /groups.json
  def index
    @state = "active"
    case params.fetch(:tab, "active")
      when "pendings"
        @state = "pending"
    end

    conds = {:state => @state, :private => false}

    if params[:q].blank?
      @groups = Group.where(conds).page(params["page"])
    else
      @groups = Group.filter(params[:q],conds)
    end

    respond_to do |format|
      format.html # index.html.haml
      format.json  { render :json => @groups }
      format.js do
        html = render_to_string(:partial => "group", :collection  => @groups)
        pagination = render_to_string(:partial => "shared/pagination", :object => @groups,
                                      :format => "html")
        render :json => {:html => html, :pagination => pagination }
      end
    end
  end

  # GET /groups/1
  # GET /groups/1.json
  def show
    @active_subtab = "about"

    if params[:id]
      @group = Group.find_by_slug_or_id(params[:id])
    else
      @group = current_group
    end
    raise Error404 if @group.nil?

    respond_to do |format|
      format.html # show.html.erb
      format.json  { render :json => @group }
    end
  end

  # GET /groups/new
  # GET /groups/new.json
  def new
    @group = Group.new

    respond_to do |format|
      format.html # new.html.erb
      format.json  { render :json => @group }
    end
  end

  # GET /groups/1/edit
  def edit
  end

  # POST /groups
  # POST /groups.json
  def create
    @group = Group.new
    @group.languages = params[:languages].split(',') if params[:languages]
    @group.safe_update(%w[name legend description default_tags subdomain logo forum enable_mathjax enable_latex
                          custom_favicon language theme signup_type custom_css wysiwyg_editor], params[:group])

    @group.safe_update(%w[isolate domain private], params[:group]) if current_user.admin?

    @group.owner = current_user
    @group.state = "active"

    respond_to do |format|
      if @group.save
        @group.create_default_widgets

        Jobs::Images.async.generate_group_thumbnails(@group.id)
        @group.add_member(current_user, "owner")
        flash[:notice] = I18n.t("groups.create.flash_notice")
        format.html { redirect_to(domain_url(:custom => @group.domain, :controller => "admin/manage", :action => "properties")) }
        format.json  { render :json => @group.to_json, :status => :created, :location => @group }
      else
        format.html { render :action => "new" }
        format.json { render :json => @group.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /groups/1
  # PUT /groups/1.json
  def update
    @group.safe_update(%w[track_users name legend description default_tags subdomain logo logo_info forum enable_latex enable_mathjax
                          custom_favicon language languages current_theme_id reputation_rewards daily_cap reputation_constrains
                          has_adult_content registered_only enable_anonymous signup_type custom_css wysiwyg_editor layout
                          fb_button notification_opts auth_providers allow_any_openid], params[:group])
    @group.share.safe_update(%w[fb_app_id fb_secret_key fb_active starts_with ends_with enable_twitter twitter_user twitter_pattern], params[:group][:share]) if params[:group][:share]
    @group.safe_update(%w[isolate domain private has_custom_analytics has_custom_html has_custom_js], params[:group]) #if current_user.admin?
    @group.safe_update(%w[analytics_id analytics_vendor], params[:group]) if @group.has_custom_analytics
    @group.custom_html.update_attributes(params[:group][:custom_html] || {}) if @group.has_custom_html
    @group.notification_opts.safe_update(%w[questions_to_twitter badges_to_twitter favorites_to_twitter answers_to_twitter comments_to_twitter], params[:group][:notification_opts]) if params[:group][:notification_opts]
    if params[:group][:language] && !params[:group]['languages']
      @group.languages = []
    end
    if @group.domain == AppConfig.domain ||
        @group.domain.index(AppConfig.domain).nil? ||
        @group.user.role == 'admin'
      @group.has_custom_js = true
    else
      @group.has_custom_js = false
    end

    if params[:group][:logo]
      @group.logo_version += 1
{'fraction_non_alphanumeric': 0.11952231555510576, 'fraction_numerical': 0.002226495293998583, 'mean_word_length': 3.048340843916428, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2474518', 'n_tokens_mistral': 3057, 'n_tokens_neox': 2970, 'n_words': 655}