Arie/serveme

View on GitHub
app/views/users/edit.html.haml

Summary

Maintainability
Test Coverage
- content_for(:title) { "settings" }

%h2 Your settings
= simple_form_for(@user, :html => { :class => 'form-horizontal' }) do |f|
  = f.input :time_zone, :include_blank => true, :hint => "Auto detection thinks you're in time zone: #{time_zone_from_cookie}", :required => true, :input_html => {:class => "select2"}, :collection => ActiveSupport::TimeZone.all, :value_method => lambda { |atz| atz.tzinfo.identifier }, :label_method => lambda { |atz| "(UTC#{atz.now.formatted_offset}) #{atz.name}" }
  - logs_tf_hint = "#{link_to('Get or generate your logs.tf API key', 'http://logs.tf/uploader', :target => '_blank')} on the logs.tf website, and enter it here".html_safe
  - demos_tf_hint = "#{link_to('Get your demos.tf API key', 'https://demos.tf/upload', :target => '_blank')} on the demos.tf website, and enter it here".html_safe
  = f.input :logs_tf_api_key, :placeholder => "logs.tf API key", :hint => logs_tf_hint, :as => "string"
  = f.input :demos_tf_api_key, :placeholder => "demos.tf API key", :hint => demos_tf_hint
  = f.input :api_key, label: "serveme.tf API key", :disabled => true, :readonly => true, :as => "string"

  .col-md-offset-2
    %button.btn.btn-primary{:type => :submit}
      Save

- if current_user.private_server_option?
  %h2 Your private server

  :javascript
    free_servers = #{(Server.active.without_group.ordered + Server.active.member_of_groups(Group.where(id: Group.private_user(current_user).id))).map { |s| {id: s.id, text: s.name, flag: s.location_flag, ip_and_port: "#{s.public_ip}:#{s.public_port}"} }.to_json.html_safe};
  = simple_form_for(@private_server, url: url_for(controller: "private_servers", action: "create"), :html => { :class => 'form' }) do |f|
    = f.input :server_id, :label => "Private server", :required => true, :input_html => { :value => (current_user.private_server && current_user.private_server.id) }

    .col-md-offset-2
      %button.btn.btn-primary{:type => :submit}
        Save private server

%h2#your-vouchers
  Your premium codes
  = link_to "Buy", donate_path(gift: true), class: "btn btn-success", data: { turbo: false }

- if @vouchers.any?

  %table.table.table-condensed
    %thead
      %tr
        %th Code
        %th Product
        %th Generated
        %th Claimed
        %th Claimed by

    - @vouchers.each do |voucher|
      %tr
        %td
          = link_to voucher.hyphenate, claim_path(code: voucher.hyphenate)
          = render 'shared/copy_button', :copy_button_id => "voucher_code_#{voucher.id}", :copy_text => claim_url(code: voucher.hyphenate)
        %td= voucher.product.name
        %td= I18n.l(voucher.created_at, format: :short)
        %td= I18n.l(voucher.claimed_at, format: :short) if voucher.claimed_at
        %td
          - if voucher.claimed_by
            - user = voucher.claimed_by
            = link_to(user.nickname, user.steam_profile_url, :target => "_blank")

= render 'statistics', :user => @user

:javascript
  $(document).on('turbo:load', function() {
    $("#user_time_zone").select2();
    $("#private_server_server_id").select2("enable", #{current_user.private_server_option?});
    $("#private_server_server_id").select2("val", $("#private_server_server_id").val());
  });

- if current_user.private_server_option?
  :javascript
    $(document).on('turbo:load', function() {
      $("#private_server_server_id").select2({
        formatResult: function(server) { return "<span class='flags flags-" + server.flag + "'></span>" + server.text; },
        formatSelection: function(server) { return "<span class='flags flags-" + server.flag + "'></span>" + server.text; },
        data: function() { return {results: free_servers}; },
        escapeMarkup: function(m) { return m }
      });
    });