ActiveAdmin.register JamRuby::User do

  menu :label => 'Jam User'

  config.sort_order = 'created_at DESC'

  filter :email
  filter :first_name
  filter :last_name
  filter :internet_service_provider
  filter :created_at
  filter :updated_at

  form do |ff|
    ff.inputs "Details" do
      ff.input :email
      ff.input :admin
      ff.input :raw_password, :label => 'Password'
      ff.input :first_name
      ff.input :last_name
      ff.input :city
      ff.input :state
      ff.input :country
      ff.input :musician
      ff.input :can_invite
      ff.input :photo_url
      ff.input :internet_service_provider
      ff.input :session_settings
    end
    ff.inputs "Signup" do
      ff.input :email_template, :label => 'Welcome Email Template Name'
      ff.input :confirm_url, :label => 'Signup Confirm URL'
    end
    ff.actions
  end

  show do |user|
    attributes_table do
      row :id
      row :email
      row :admin
      row :updated_at
      row :created_at
      row :musician
      row :city
      row :state
      row :country
      row :first_name
      row :last_name
      row :birth_date
      row :gender
      row :internet_service_provider
      row :email_confirmed
      row :image do user.photo_url ? image_tag(user.photo_url) : ''  end
      row :session_settings
      row :can_invite
    end
    active_admin_comments
  end
  index do
    column "ID" do |user|
      link_to(truncate(user.id, {:length => 12}), 
              resource_path(user), 
              {:title => user.id})
    end
    column "Email" do |user|
      link_to user.email, resource_path(user)
    end
    column :admin
    column :updated_at
    column :created_at
    column :musician
    column :city
    column :state
    column :country
    column :first_name
    column :last_name
    column :birth_date
    column :gender
    column :internet_service_provider
    column :email_confirmed
    column :photo_url
    column :session_settings
    column :can_invite

    # default_actions # use this for all view/edit/delete links
    column "Actions" do |user|
      links = ''.html_safe
      links << link_to("View", resource_path(user), :class => "member_link view_link")
      links << link_to("Edit", edit_resource_path(user), :class => "member_link edit_link")
      links
    end
  end


  controller do

    def create
      @jam_ruby_user = JamRuby::User.new(params[:jam_ruby_user])
      @jam_ruby_user.administratively_created = true
      if @jam_ruby_user.password.nil? || @jam_ruby_user.password.length == 0
        # a nil password in the form  means we simply won't create one; however,
        # the password_digest has to be set to something to make 'has_secure_password' happy
        @jam_ruby_user.password_digest = SecureRandom.urlsafe_base64
      end

      # call `create!` to ensure that the rest of the action continues as normal
      create!
    end
  end

end
