Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add Ace editor for editing JavaScript in credentials

  • Loading branch information...
commit c043a4086884af9639cd8c3b56a1342bd3991d0b 1 parent 62b60d1
@cantino authored
View
1  Gemfile
@@ -24,6 +24,7 @@ gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
gem 'select2-rails'
gem 'jquery-rails'
+gem 'ace-rails-ap'
gem 'geokit-rails3'
gem 'kramdown'
View
2  Gemfile.lock
@@ -1,6 +1,7 @@
GEM
remote: https://rubygems.org/
specs:
+ ace-rails-ap (2.0.1)
actionmailer (3.2.13)
actionpack (= 3.2.13)
mail (~> 2.5.3)
@@ -272,6 +273,7 @@ PLATFORMS
ruby
DEPENDENCIES
+ ace-rails-ap
better_errors
binding_of_caller
bootstrap-kaminari-views
View
27 app/assets/javascripts/user_credentials.js.coffee
@@ -0,0 +1,27 @@
+#= require ace/ace
+#= require ace/mode-javascript.js
+#= require ace/mode-markdown.js
+#= require_self
+
+$ ->
+ editor = ace.edit("ace-credential-value")
+ editor.getSession().setTabSize(2)
+ editor.getSession().setUseSoftTabs(true)
+ editor.getSession().setUseWrapMode(false)
+ editor.setTheme("ace/theme/chrome")
+
+ setMode = ->
+ mode = $("#user_credential_mode").val()
+ if mode == 'java_script'
+ editor.getSession().setMode("ace/mode/javascript")
+ else
+ editor.getSession().setMode("ace/mode/text")
+
+ setMode()
+ $("#user_credential_mode").on 'change', setMode
+
+ $textarea = $('#user_credential_credential_value').hide()
+ editor.getSession().setValue($textarea.val())
+
+ $textarea.closest('form').on 'submit', ->
+ $textarea.val(editor.getSession().getValue())
View
8 app/assets/stylesheets/application.css.scss.erb
@@ -126,3 +126,11 @@ span.not-applicable:after {
#show-tabs li a.recent-errors {
font-weight: bold;
}
+
+// Credentials
+
+#ace-credential-value {
+ position: relative;
+ width: 940px;
+ height: 400px;
+}
View
10 app/models/user_credential.rb
@@ -1,13 +1,17 @@
class UserCredential < ActiveRecord::Base
- attr_accessible :credential_name, :credential_value
+ MODES = %w[text java_script]
+
+ attr_accessible :credential_name, :credential_value, :mode
belongs_to :user
validates_presence_of :credential_name
validates_presence_of :credential_value
+ validates_inclusion_of :mode, :in => MODES
validates_presence_of :user_id
validates_uniqueness_of :credential_name, :scope => :user_id
+ before_validation :default_mode_to_text
before_save :trim_fields
protected
@@ -16,4 +20,8 @@ def trim_fields
credential_name.strip!
credential_value.strip!
end
+
+ def default_mode_to_text
+ self.mode = 'text' unless mode.present?
+ end
end
View
1  app/views/agents/_form.html.erb
@@ -44,7 +44,6 @@
</div>
</div>
-
<div class='event-related-region' data-can-create-events="<%= @agent.can_create_events? %>">
<div class="control-group">
<%= f.label :keep_events_for, "Keep events", :class => 'control-label' %>
View
10 app/views/user_credentials/_form.html.erb
@@ -18,9 +18,17 @@
</div>
<div class="control-group">
+ <%= f.label :mode, :class => 'control-label' %>
+ <div class="controls">
+ <%= f.select :mode, options_for_select(UserCredential::MODES.map {|s| [s.classify, s] }, @user_credential.mode), {}, :class => 'span4' %>
+ </div>
+ </div>
+
+ <div class="control-group">
<%= f.label :credential_value, :class => 'control-label' %>
<div class="controls">
<%= f.text_area :credential_value, :class => 'span8', :rows => 10 %>
+ <div id="ace-credential-value"></div>
</div>
</div>
@@ -28,3 +36,5 @@
<%= f.submit "Save Credential", :class => "btn btn-primary" %>
</div>
<% end %>
+
+<%= javascript_include_tag "user_credentials" %>
View
2  config/environments/production.rb
@@ -48,7 +48,7 @@
end
# Precompile additional assets (application.js.coffee.erb, application.css, and all non-JS/CSS are already added)
- config.assets.precompile += %w( graphing.js )
+ config.assets.precompile += %w( graphing.js user_credentials.js )
# Enable threaded mode
# config.threadsafe!
View
5 db/migrate/20140210062747_add_mode_to_user_credentials.rb
@@ -0,0 +1,5 @@
+class AddModeToUserCredentials < ActiveRecord::Migration
+ def change
+ add_column :user_credentials, :mode, :string, :default => 'text', :null => false
+ end
+end
View
13 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20140127164931) do
+ActiveRecord::Schema.define(:version => 20140210062747) do
create_table "agent_logs", :force => true do |t|
t.integer "agent_id", :null => false
@@ -96,11 +96,12 @@
add_index "links", ["source_id", "receiver_id"], :name => "index_links_on_source_id_and_receiver_id"
create_table "user_credentials", :force => true do |t|
- t.integer "user_id", :null => false
- t.string "credential_name", :null => false
- t.text "credential_value", :null => false
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.integer "user_id", :null => false
+ t.string "credential_name", :null => false
+ t.text "credential_value", :null => false
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ t.string "mode", :default => "text", :null => false
end
add_index "user_credentials", ["user_id", "credential_name"], :name => "index_user_credentials_on_user_id_and_credential_name", :unique => true
View
4 spec/fixtures/user_credentials.yml
@@ -2,15 +2,19 @@ bob_aws_key:
user: bob
credential_name: aws_key
credential_value: 2222222222-bob
+ mode: text
bob_aws_secret:
user: bob
credential_name: aws_secret
credential_value: 1111111111-bob
+ mode: text
jane_aws_key:
user: jane
credential_name: aws_key
credential_value: 2222222222-jane
+ mode: text
jane_aws_secret:
user: jane
credential_name: aws_secret
credential_value: 1111111111-jabe
+ mode: text
Please sign in to comment.
Something went wrong with that request. Please try again.