Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding warden

  • Loading branch information...
commit f3e224ad94124c21583aab1ccc3083a56167b3ad 1 parent 1b81483
Robert Sosinski authored
5 Gemfile
View
@@ -7,8 +7,11 @@ gem 'rails', '3.2.2'
gem 'pg'
gem 'json'
gem 'rjson'
-gem 'devise', '~> 2.0.4'
+
+gem 'warden', '~> 1.1.1'
gem 'bcrypt-ruby', '~> 3.0.0'
+
+gem 'simple_form', '~> 2.0.1'
gem 'whenever', :require => false
group :assets do
12 Gemfile.lock
View
@@ -46,11 +46,6 @@ GEM
columnize (0.3.4)
commonjs (0.2.0)
therubyracer (~> 0.9.9)
- devise (2.0.4)
- bcrypt-ruby (~> 3.0)
- orm_adapter (~> 0.0.3)
- railties (~> 3.1)
- warden (~> 1.1.1)
diff-lcs (1.1.3)
erubis (2.7.0)
execjs (1.3.0)
@@ -82,7 +77,6 @@ GEM
treetop (~> 1.4.8)
mime-types (1.17.2)
multi_json (1.1.0)
- orm_adapter (0.0.6)
pg (0.13.2)
polyglot (0.3.3)
rack (1.4.1)
@@ -128,6 +122,9 @@ GEM
ruby-debug-base (~> 0.10.4.0)
ruby-debug-base (0.10.4)
linecache (>= 0.3)
+ simple_form (2.0.1)
+ actionpack (~> 3.0)
+ activemodel (~> 3.0)
sprockets (2.1.2)
hike (~> 1.2)
rack (~> 1.0)
@@ -154,7 +151,6 @@ PLATFORMS
DEPENDENCIES
bcrypt-ruby (~> 3.0.0)
- devise
factory_girl_rails
json
less (~> 2.0.10)
@@ -163,5 +159,7 @@ DEPENDENCIES
rjson
rspec-rails!
ruby-debug
+ simple_form (~> 2.0.1)
uglifier
+ warden (~> 1.1.1)
whenever
1  app/assets/javascripts/application.js
View
@@ -4,6 +4,7 @@
//= require knockout-2.0.0
//= require cookies
+//= require rails
//= require ko
//= require_self
4 app/controllers/application_controller.rb
View
@@ -1,2 +1,6 @@
class ApplicationController < ActionController::Base
+ def warden
+ env['warden']
+ end
+ helper_method :warden
end
24 app/controllers/root/sessions_controller.rb
View
@@ -1,2 +1,26 @@
class Root::SessionsController < Root::NamespaceController
+ def show
+ render :text => session.inspect
+ end
+
+ def new
+ end
+
+ def create
+ warden.authenticate!
+
+ render :text => session.inspect
+ end
+
+ def destroy
+ warden.logout
+
+ render :text => session.inspect
+ end
+
+ def failure
+ flash.now[:error] = warden.message
+
+ render :new
+ end
end
5 app/controllers/root/users_controller.rb
View
@@ -1,2 +1,7 @@
class Root::UsersController < Root::NamespaceController
+ def new
+ end
+
+ def create
+ end
end
15 app/models/user.rb
View
@@ -1,14 +1,21 @@
class User < ActiveRecord::Base
has_secure_password
- attr_accessible :name, :password, :password_confirmation
+ attr_accessible :username, :password, :password_confirmation
has_many :tasks
- validates :name, :presence => true,
- :uniqueness => true,
- :format => { :with => /^[a-z0-9]{3,10}$/, :message => "must be 3 to 10 lower-case letters or numbers" }
+ validates :username, :presence => true,
+ :uniqueness => true,
+ :format => { :with => /^[a-z0-9]{3,10}$/, :message => "must be 3 to 10 lower-case letters or numbers" }
validates :password, :confirmation => true,
:format => { :with => /^([\x20-\x7E]){6,}$/, :message => "must be 3 or more characters" }
+
+
+ def self.authenticate(username, password)
+ find_by_username!(username).authenticate(password)
+ rescue
+ false
+ end
end
18 app/views/root/layouts/_admin_nav.erb
View
@@ -0,0 +1,18 @@
+<div class="navbar-inner">
+ <div class="container">
+ <%= link_to "Kiv7", root_path, :class => "brand" %>
+ <ul class="nav">
+ <li>
+ <%= link_to "About", pages_path(:action => :about) %>
+ </li>
+ <li>
+ <%= link_to "Admin", admin_stats_path %>
+ </li>
+ </ul>
+ <ul class="nav pull-right">
+ <li>
+ <%= link_to "Logout", sessions_path, :method => :delete %>
+ </li>
+ </ul>
+ </div>
+</div>
18 app/views/root/layouts/_guest_nav.erb
View
@@ -0,0 +1,18 @@
+<div class="navbar-inner">
+ <div class="container">
+ <%= link_to "Kiv7", root_path, :class => "brand" %>
+ <ul class="nav">
+ <li>
+ <%= link_to "About", pages_path(:action => :about) %>
+ </li>
+ </ul>
+ <ul class="nav pull-right">
+ <li>
+ <%= link_to "Login", new_sessions_path %>
+ </li>
+ <li>
+ <%= link_to "Signup", new_users_path %>
+ </li>
+ </ul>
+ </div>
+</div>
15 app/views/root/layouts/_user_nav.erb
View
@@ -0,0 +1,15 @@
+<div class="navbar-inner">
+ <div class="container">
+ <%= link_to "Kiv7", root_path, :class => "brand" %>
+ <ul class="nav">
+ <li>
+ <%= link_to "About", pages_path(:action => :about) %>
+ </li>
+ </ul>
+ <ul class="nav pull-right">
+ <li>
+ <%= link_to "Logout", sessions_path, :method => :delete %>
+ </li>
+ </ul>
+ </div>
+</div>
42 app/views/root/layouts/namespace.html.erb
View
@@ -10,33 +10,27 @@
<body>
<div id="navbar" class="navbar navbar-fixed-top">
- <div class="navbar-inner">
- <div class="container">
- <a class="brand" href="#">Kiv7</a>
- <ul class="nav">
- <li>
- <a href="#">About</a>
- </li>
- <li>
- <a href="#">Lists</a>
- </li>
- <li>
- <a href="#">Account</a>
- </li>
- </ul>
- <form class="navbar-search pull-left">
- <input type="text" class="search-query" placeholder="Filter Tasks">
- </form>
- <ul class="nav pull-right">
- <li>
- <a href="#">Logout</a>
- </li>
- </ul>
- </div>
- </div>
+ <% if warden.authenticated? %>
+ <% if warden.user.admin? %>
+ <%= render :partial => 'root/layouts/admin_nav' %>
+ <% else %>
+ <%= render :partial => 'root/layouts/user_nav' %>
+ <% end %>
+ <% else %>
+ <%= render :partial => 'root/layouts/guest_nav' %>
+ <% end %>
</div>
<div id="content" class="container">
+ <% if flash.any? %>
+ <% flash.each do |type, message| %>
+ <%= content_tag :div, :class => ["alert", "alert-#{type}"] do%>
+ <a class="close" data-dismiss="alert">×</a>
+ <strong><%= message %></strong>
+ <% end %>
+ <% end %>
+ <% end %>
+
<%= yield %>
</div>
</body>
7 app/views/root/pages/about.html.erb
View
@@ -1 +1,6 @@
-<h1>About</h1>
+<div class="page-header">
+ <h1>
+ About
+ <small>the site you are looking at, right now</small>
+ </h1>
+</div>
7 app/views/root/pages/welcome.html.erb
View
@@ -1 +1,6 @@
-<h1>Welcome<h1>
+<div class="page-header">
+ <h1>
+ Welcome
+ <small>to a useful task-tracking application</small>
+ </h1>
+</div>
15 app/views/root/sessions/new.html.erb
View
@@ -0,0 +1,15 @@
+<div class="page-header">
+ <h1>
+ Login
+ <small>if you already have an account</small>
+ </h1>
+</div>
+
+<%= simple_form_for :session, :url => sessions_path, :html => { :class => "form-horizontal" } do |form| %>
+ <%= form.input :username %>
+ <%= form.input :password %>
+
+ <%= content_tag :div, :class => "form-actions" do %>
+ <%= form.button :submit, :value => "Login", :class => "btn-primary" %>
+ <% end %>
+<% end %>
176 config/initializers/simple_form.rb
View
@@ -0,0 +1,176 @@
+# Use this setup block to configure all options available in SimpleForm.
+SimpleForm.setup do |config|
+ # Wrappers are used by the form builder to generate a
+ # complete input. You can remove any component from the
+ # wrapper, change the order or even add your own to the
+ # stack. The options given below are used to wrap the
+ # whole input.
+ config.wrappers :default, :class => :input,
+ :hint_class => :field_with_hint, :error_class => :field_with_errors do |b|
+ ## Extensions enabled by default
+ # Any of these extensions can be disabled for a
+ # given input by passing: `f.input EXTENSION_NAME => false`.
+ # You can make any of these extensions optional by
+ # renaming `b.use` to `b.optional`.
+
+ # Determines whether to use HTML5 (:email, :url, ...)
+ # and required attributes
+ b.use :html5
+
+ # Calculates placeholders automatically from I18n
+ # You can also pass a string as f.input :placeholder => "Placeholder"
+ b.use :placeholder
+
+ ## Optional extensions
+ # They are disabled unless you pass `f.input EXTENSION_NAME => :lookup`
+ # to the input. If so, they will retrieve the values from the model
+ # if any exists. If you want to enable the lookup for any of those
+ # extensions by default, you can change `b.optional` to `b.use`.
+
+ # Calculates maxlength from length validations for string inputs
+ b.optional :maxlength
+
+ # Calculates pattern from format validations for string inputs
+ b.optional :pattern
+
+ # Calculates min and max from length validations for numeric inputs
+ b.optional :min_max
+
+ # Calculates readonly automatically from readonly attributes
+ b.optional :readonly
+
+ ## Inputs
+ b.use :label_input
+ b.use :hint, :wrap_with => { :tag => :span, :class => :hint }
+ b.use :error, :wrap_with => { :tag => :span, :class => :error }
+ end
+
+ config.wrappers :bootstrap, :tag => 'div', :class => 'control-group', :error_class => 'error' do |b|
+ b.use :html5
+ b.use :placeholder
+ b.use :label
+ b.wrapper :tag => 'div', :class => 'controls' do |ba|
+ ba.use :input
+ ba.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
+ ba.use :hint, :wrap_with => { :tag => 'p', :class => 'help-block' }
+ end
+ end
+
+ config.wrappers :prepend, :tag => 'div', :class => "control-group", :error_class => 'error' do |b|
+ b.use :html5
+ b.use :placeholder
+ b.use :label
+ b.wrapper :tag => 'div', :class => 'controls' do |input|
+ input.wrapper :tag => 'div', :class => 'input-prepend' do |prepend|
+ prepend.use :input
+ end
+ input.use :hint, :wrap_with => { :tag => 'span', :class => 'help-block' }
+ input.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
+ end
+ end
+
+ config.wrappers :append, :tag => 'div', :class => "control-group", :error_class => 'error' do |b|
+ b.use :html5
+ b.use :placeholder
+ b.use :label
+ b.wrapper :tag => 'div', :class => 'controls' do |input|
+ input.wrapper :tag => 'div', :class => 'input-append' do |append|
+ append.use :input
+ end
+ input.use :hint, :wrap_with => { :tag => 'span', :class => 'help-block' }
+ input.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
+ end
+ end
+
+ # Wrappers for forms and inputs using the Twitter Bootstrap toolkit.
+ # Check the Bootstrap docs (http://twitter.github.com/bootstrap)
+ # to learn about the different styles for forms and inputs,
+ # buttons and other elements.
+ config.default_wrapper = :bootstrap
+
+ # Define the way to render check boxes / radio buttons with labels.
+ # Defaults to :nested for bootstrap config.
+ # :inline => input + label
+ # :nested => label > input
+ config.boolean_style = :nested
+
+ # Default class for buttons
+ config.button_class = 'btn'
+
+ # Method used to tidy up errors.
+ # config.error_method = :first
+
+ # Default tag used for error notification helper.
+ config.error_notification_tag = :div
+
+ # CSS class to add for error notification helper.
+ config.error_notification_class = 'alert alert-error'
+
+ # ID to add for error notification helper.
+ # config.error_notification_id = nil
+
+ # Series of attempts to detect a default label method for collection.
+ # config.collection_label_methods = [ :to_label, :name, :title, :to_s ]
+
+ # Series of attempts to detect a default value method for collection.
+ # config.collection_value_methods = [ :id, :to_s ]
+
+ # You can wrap a collection of radio/check boxes in a pre-defined tag, defaulting to none.
+ # config.collection_wrapper_tag = nil
+
+ # You can define the class to use on all collection wrappers. Defaulting to none.
+ # config.collection_wrapper_class = nil
+
+ # You can wrap each item in a collection of radio/check boxes with a tag,
+ # defaulting to :span. Please note that when using :boolean_style = :nested,
+ # SimpleForm will force this option to be a label.
+ # config.item_wrapper_tag = :span
+
+ # You can define a class to use in all item wrappers. Defaulting to none.
+ # config.item_wrapper_class = nil
+
+ # How the label text should be generated altogether with the required text.
+ # config.label_text = lambda { |label, required| "#{required} #{label}" }
+
+ # You can define the class to use on all labels. Default is nil.
+ config.label_class = 'control-label'
+
+ # You can define the class to use on all forms. Default is simple_form.
+ # config.form_class = :simple_form
+
+ # You can define which elements should obtain additional classes
+ # config.generate_additional_classes_for = [:wrapper, :label, :input]
+
+ # Whether attributes are required by default (or not). Default is true.
+ # config.required_by_default = true
+
+ # Tell browsers whether to use default HTML5 validations (novalidate option).
+ # Default is enabled.
+ config.browser_validations = false
+
+ # Collection of methods to detect if a file type was given.
+ # config.file_methods = [ :mounted_as, :file?, :public_filename ]
+
+ # Custom mappings for input types. This should be a hash containing a regexp
+ # to match as key, and the input type that will be used when the field name
+ # matches the regexp as value.
+ # config.input_mappings = { /count/ => :integer }
+
+ # Default priority for time_zone inputs.
+ # config.time_zone_priority = nil
+
+ # Default priority for country inputs.
+ # config.country_priority = nil
+
+ # Default size for text inputs.
+ # config.default_input_size = 50
+
+ # When false, do not use translations for labels.
+ # config.translate_labels = true
+
+ # Automatically discover new inputs in Rails' autoload path.
+ # config.inputs_discovery = true
+
+ # Cache SimpleForm inputs discovery
+ # config.cache_discovery = !Rails.env.development?
+end
23 config/initializers/warden.rb
View
@@ -0,0 +1,23 @@
+Rails.configuration.middleware.use Warden::Manager do |manager|
+ manager.default_strategies :username_password
+ manager.failure_app = Root::SessionsController.action(:failure)
+end
+
+Warden::Manager.serialize_into_session do |user|
+ user.id
+end
+
+Warden::Manager.serialize_from_session do |id|
+ User.find_by_id(id)
+end
+
+Warden::Strategies.add(:username_password) do
+ def valid?
+ params["session"] && params["session"]["username"] && params["session"]["password"]
+ end
+
+ def authenticate!
+ user = User.authenticate(params["session"]["username"], params["session"]["password"])
+ user ? success!(user) : fail!("Invalid username and/or password.")
+ end
+end
24 config/locales/simple_form.en.yml
View
@@ -0,0 +1,24 @@
+en:
+ simple_form:
+ "yes": 'Yes'
+ "no": 'No'
+ required:
+ text: 'required'
+ mark: '*'
+ # You can uncomment the line below if you need to overwrite the whole required html.
+ # When using html, text and mark won't be used.
+ # html: '<abbr title="required">*</abbr>'
+ error_notification:
+ default_message: "Some errors were found, please take a look:"
+ # Labels and hints examples
+ # labels:
+ # password: 'Password'
+ # user:
+ # new:
+ # email: 'E-mail para efetuar o sign in.'
+ # edit:
+ # email: 'E-mail.'
+ # hints:
+ # username: 'User name to sign in.'
+ # password: 'No special characters, please.'
+
2  config/routes/root.rb
View
@@ -3,7 +3,7 @@
root :to => 'pages#welcome'
# routes all pages to an appropriate template
- match 'pages/:action', :controller => 'pages'
+ match 'pages/:action', :controller => 'pages', :as => :pages
resource :sessions
resource :users
2  db/migrate/20120312035515_create_users.rb
View
@@ -3,7 +3,7 @@ def up
execute <<-SQL
create table users (
id serial primary key,
- name varchar(10) unique not null,
+ username varchar(10) unique not null,
password_digest varchar(60) not null,
admin boolean default false not null,
created_at timestamp,
4 db/seeds.rb
View
@@ -1,6 +1,6 @@
-alice = User.create(:name => 'alice', :password => 'hushhush')
+alice = User.create(:username => 'alice', :password => 'hushhush')
-bob = User.new(:name => 'bob', :password => 'password')
+bob = User.new(:username => 'bob', :password => 'password')
bob.admin = true
bob.save
42 db/structure.sql
View
@@ -71,23 +71,12 @@ ALTER SEQUENCE tasks_id_seq OWNED BY tasks.id;
--
--- Name: test; Type: TABLE; Schema: public; Owner: -; Tablespace:
---
-
-CREATE TABLE test (
- a integer NOT NULL,
- b integer,
- c integer
-);
-
-
---
-- Name: users; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
CREATE TABLE users (
id integer NOT NULL,
- name character varying(10) NOT NULL,
+ username character varying(10) NOT NULL,
password_digest character varying(60) NOT NULL,
admin boolean DEFAULT false NOT NULL,
created_at timestamp without time zone,
@@ -137,30 +126,6 @@ ALTER TABLE ONLY tasks
--
--- Name: test_b_key; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
---
-
-ALTER TABLE ONLY test
- ADD CONSTRAINT test_b_key UNIQUE (b);
-
-
---
--- Name: test_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
---
-
-ALTER TABLE ONLY test
- ADD CONSTRAINT test_pkey PRIMARY KEY (a);
-
-
---
--- Name: users_name_key; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
---
-
-ALTER TABLE ONLY users
- ADD CONSTRAINT users_name_key UNIQUE (name);
-
-
---
-- Name: users_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
--
@@ -169,10 +134,11 @@ ALTER TABLE ONLY users
--
--- Name: test_c_key; Type: INDEX; Schema: public; Owner: -; Tablespace:
+-- Name: users_username_key; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
--
-CREATE UNIQUE INDEX test_c_key ON test USING btree (c);
+ALTER TABLE ONLY users
+ ADD CONSTRAINT users_username_key UNIQUE (username);
--
373 lib/assets/javascripts/rails.js
View
@@ -0,0 +1,373 @@
+(function($, undefined) {
+
+/**
+ * Unobtrusive scripting adapter for jQuery
+ *
+ * Requires jQuery 1.6.0 or later.
+ * https://github.com/rails/jquery-ujs
+
+ * Uploading file using rails.js
+ * =============================
+ *
+ * By default, browsers do not allow files to be uploaded via AJAX. As a result, if there are any non-blank file fields
+ * in the remote form, this adapter aborts the AJAX submission and allows the form to submit through standard means.
+ *
+ * The `ajax:aborted:file` event allows you to bind your own handler to process the form submission however you wish.
+ *
+ * Ex:
+ * $('form').live('ajax:aborted:file', function(event, elements){
+ * // Implement own remote file-transfer handler here for non-blank file inputs passed in `elements`.
+ * // Returning false in this handler tells rails.js to disallow standard form submission
+ * return false;
+ * });
+ *
+ * The `ajax:aborted:file` event is fired when a file-type input is detected with a non-blank value.
+ *
+ * Third-party tools can use this hook to detect when an AJAX file upload is attempted, and then use
+ * techniques like the iframe method to upload the file instead.
+ *
+ * Required fields in rails.js
+ * ===========================
+ *
+ * If any blank required inputs (required="required") are detected in the remote form, the whole form submission
+ * is canceled. Note that this is unlike file inputs, which still allow standard (non-AJAX) form submission.
+ *
+ * The `ajax:aborted:required` event allows you to bind your own handler to inform the user of blank required inputs.
+ *
+ * !! Note that Opera does not fire the form's submit event if there are blank required inputs, so this event may never
+ * get fired in Opera. This event is what causes other browsers to exhibit the same submit-aborting behavior.
+ *
+ * Ex:
+ * $('form').live('ajax:aborted:required', function(event, elements){
+ * // Returning false in this handler tells rails.js to submit the form anyway.
+ * // The blank required inputs are passed to this function in `elements`.
+ * return ! confirm("Would you like to submit the form with missing info?");
+ * });
+ */
+
+ // Shorthand to make it a little easier to call public rails functions from within rails.js
+ var rails;
+
+ $.rails = rails = {
+ // Link elements bound by jquery-ujs
+ linkClickSelector: 'a[data-confirm], a[data-method], a[data-remote], a[data-disable-with]',
+
+ // Select elements bound by jquery-ujs
+ inputChangeSelector: 'select[data-remote], input[data-remote], textarea[data-remote]',
+
+ // Form elements bound by jquery-ujs
+ formSubmitSelector: 'form',
+
+ // Form input elements bound by jquery-ujs
+ formInputClickSelector: 'form input[type=submit], form input[type=image], form button[type=submit], form button:not(button[type])',
+
+ // Form input elements disabled during form submission
+ disableSelector: 'input[data-disable-with], button[data-disable-with], textarea[data-disable-with]',
+
+ // Form input elements re-enabled after form submission
+ enableSelector: 'input[data-disable-with]:disabled, button[data-disable-with]:disabled, textarea[data-disable-with]:disabled',
+
+ // Form required input elements
+ requiredInputSelector: 'input[name][required]:not([disabled]),textarea[name][required]:not([disabled])',
+
+ // Form file input elements
+ fileInputSelector: 'input:file',
+
+ // Link onClick disable selector with possible reenable after remote submission
+ linkDisableSelector: 'a[data-disable-with]',
+
+ // Make sure that every Ajax request sends the CSRF token
+ CSRFProtection: function(xhr) {
+ var token = $('meta[name="csrf-token"]').attr('content');
+ if (token) xhr.setRequestHeader('X-CSRF-Token', token);
+ },
+
+ // Triggers an event on an element and returns false if the event result is false
+ fire: function(obj, name, data) {
+ var event = $.Event(name);
+ obj.trigger(event, data);
+ return event.result !== false;
+ },
+
+ // Default confirm dialog, may be overridden with custom confirm dialog in $.rails.confirm
+ confirm: function(message) {
+ return confirm(message);
+ },
+
+ // Default ajax function, may be overridden with custom function in $.rails.ajax
+ ajax: function(options) {
+ return $.ajax(options);
+ },
+
+ // Submits "remote" forms and links with ajax
+ handleRemote: function(element) {
+ var method, url, data,
+ crossDomain = element.data('cross-domain') || null,
+ dataType = element.data('type') || ($.ajaxSettings && $.ajaxSettings.dataType),
+ options;
+
+ if (rails.fire(element, 'ajax:before')) {
+
+ if (element.is('form')) {
+ method = element.attr('method');
+ url = element.attr('action');
+ data = element.serializeArray();
+ // memoized value from clicked submit button
+ var button = element.data('ujs:submit-button');
+ if (button) {
+ data.push(button);
+ element.data('ujs:submit-button', null);
+ }
+ } else if (element.is(rails.inputChangeSelector)) {
+ method = element.data('method');
+ url = element.data('url');
+ data = element.serialize();
+ if (element.data('params')) data = data + "&" + element.data('params');
+ } else {
+ method = element.data('method');
+ url = element.attr('href');
+ data = element.data('params') || null;
+ }
+
+ options = {
+ type: method || 'GET', data: data, dataType: dataType, crossDomain: crossDomain,
+ // stopping the "ajax:beforeSend" event will cancel the ajax request
+ beforeSend: function(xhr, settings) {
+ if (settings.dataType === undefined) {
+ xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script);
+ }
+ return rails.fire(element, 'ajax:beforeSend', [xhr, settings]);
+ },
+ success: function(data, status, xhr) {
+ element.trigger('ajax:success', [data, status, xhr]);
+ },
+ complete: function(xhr, status) {
+ element.trigger('ajax:complete', [xhr, status]);
+ },
+ error: function(xhr, status, error) {
+ element.trigger('ajax:error', [xhr, status, error]);
+ }
+ };
+ // Only pass url to `ajax` options if not blank
+ if (url) { options.url = url; }
+
+ return rails.ajax(options);
+ } else {
+ return false;
+ }
+ },
+
+ // Handles "data-method" on links such as:
+ // <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
+ handleMethod: function(link) {
+ var href = link.attr('href'),
+ method = link.data('method'),
+ target = link.attr('target'),
+ csrf_token = $('meta[name=csrf-token]').attr('content'),
+ csrf_param = $('meta[name=csrf-param]').attr('content'),
+ form = $('<form method="post" action="' + href + '"></form>'),
+ metadata_input = '<input name="_method" value="' + method + '" type="hidden" />';
+
+ if (csrf_param !== undefined && csrf_token !== undefined) {
+ metadata_input += '<input name="' + csrf_param + '" value="' + csrf_token + '" type="hidden" />';
+ }
+
+ if (target) { form.attr('target', target); }
+
+ form.hide().append(metadata_input).appendTo('body');
+ form.submit();
+ },
+
+ /* Disables form elements:
+ - Caches element value in 'ujs:enable-with' data store
+ - Replaces element text with value of 'data-disable-with' attribute
+ - Sets disabled property to true
+ */
+ disableFormElements: function(form) {
+ form.find(rails.disableSelector).each(function() {
+ var element = $(this), method = element.is('button') ? 'html' : 'val';
+ element.data('ujs:enable-with', element[method]());
+ element[method](element.data('disable-with'));
+ element.prop('disabled', true);
+ });
+ },
+
+ /* Re-enables disabled form elements:
+ - Replaces element text with cached value from 'ujs:enable-with' data store (created in `disableFormElements`)
+ - Sets disabled property to false
+ */
+ enableFormElements: function(form) {
+ form.find(rails.enableSelector).each(function() {
+ var element = $(this), method = element.is('button') ? 'html' : 'val';
+ if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with'));
+ element.prop('disabled', false);
+ });
+ },
+
+ /* For 'data-confirm' attribute:
+ - Fires `confirm` event
+ - Shows the confirmation dialog
+ - Fires the `confirm:complete` event
+
+ Returns `true` if no function stops the chain and user chose yes; `false` otherwise.
+ Attaching a handler to the element's `confirm` event that returns a `falsy` value cancels the confirmation dialog.
+ Attaching a handler to the element's `confirm:complete` event that returns a `falsy` value makes this function
+ return false. The `confirm:complete` event is fired whether or not the user answered true or false to the dialog.
+ */
+ allowAction: function(element) {
+ var message = element.data('confirm'),
+ answer = false, callback;
+ if (!message) { return true; }
+
+ if (rails.fire(element, 'confirm')) {
+ answer = rails.confirm(message);
+ callback = rails.fire(element, 'confirm:complete', [answer]);
+ }
+ return answer && callback;
+ },
+
+ // Helper function which checks for blank inputs in a form that match the specified CSS selector
+ blankInputs: function(form, specifiedSelector, nonBlank) {
+ var inputs = $(), input,
+ selector = specifiedSelector || 'input,textarea';
+ form.find(selector).each(function() {
+ input = $(this);
+ // Collect non-blank inputs if nonBlank option is true, otherwise, collect blank inputs
+ if (nonBlank ? input.val() : !input.val()) {
+ inputs = inputs.add(input);
+ }
+ });
+ return inputs.length ? inputs : false;
+ },
+
+ // Helper function which checks for non-blank inputs in a form that match the specified CSS selector
+ nonBlankInputs: function(form, specifiedSelector) {
+ return rails.blankInputs(form, specifiedSelector, true); // true specifies nonBlank
+ },
+
+ // Helper function, needed to provide consistent behavior in IE
+ stopEverything: function(e) {
+ $(e.target).trigger('ujs:everythingStopped');
+ e.stopImmediatePropagation();
+ return false;
+ },
+
+ // find all the submit events directly bound to the form and
+ // manually invoke them. If anyone returns false then stop the loop
+ callFormSubmitBindings: function(form, event) {
+ var events = form.data('events'), continuePropagation = true;
+ if (events !== undefined && events['submit'] !== undefined) {
+ $.each(events['submit'], function(i, obj){
+ if (typeof obj.handler === 'function') return continuePropagation = obj.handler(event);
+ });
+ }
+ return continuePropagation;
+ },
+
+ // replace element's html with the 'data-disable-with' after storing original html
+ // and prevent clicking on it
+ disableElement: function(element) {
+ element.data('ujs:enable-with', element.html()); // store enabled state
+ element.html(element.data('disable-with')); // set to disabled state
+ element.bind('click.railsDisable', function(e) { // prevent further clicking
+ return rails.stopEverything(e)
+ });
+ },
+
+ // restore element to its original state which was disabled by 'disableElement' above
+ enableElement: function(element) {
+ if (element.data('ujs:enable-with') !== undefined) {
+ element.html(element.data('ujs:enable-with')); // set to old enabled state
+ // this should be element.removeData('ujs:enable-with')
+ // but, there is currently a bug in jquery which makes hyphenated data attributes not get removed
+ element.data('ujs:enable-with', false); // clean up cache
+ }
+ element.unbind('click.railsDisable'); // enable element
+ }
+
+ };
+
+ $.ajaxPrefilter(function(options, originalOptions, xhr){ if ( !options.crossDomain ) { rails.CSRFProtection(xhr); }});
+
+ $(document).delegate(rails.linkDisableSelector, 'ajax:complete', function() {
+ rails.enableElement($(this));
+ });
+
+ $(document).delegate(rails.linkClickSelector, 'click.rails', function(e) {
+ var link = $(this), method = link.data('method'), data = link.data('params');
+ if (!rails.allowAction(link)) return rails.stopEverything(e);
+
+ if (link.is(rails.linkDisableSelector)) rails.disableElement(link);
+
+ if (link.data('remote') !== undefined) {
+ if ( (e.metaKey || e.ctrlKey) && (!method || method === 'GET') && !data ) { return true; }
+
+ if (rails.handleRemote(link) === false) { rails.enableElement(link); }
+ return false;
+
+ } else if (link.data('method')) {
+ rails.handleMethod(link);
+ return false;
+ }
+ });
+
+ $(document).delegate(rails.inputChangeSelector, 'change.rails', function(e) {
+ var link = $(this);
+ if (!rails.allowAction(link)) return rails.stopEverything(e);
+
+ rails.handleRemote(link);
+ return false;
+ });
+
+ $(document).delegate(rails.formSubmitSelector, 'submit.rails', function(e) {
+ var form = $(this),
+ remote = form.data('remote') !== undefined,
+ blankRequiredInputs = rails.blankInputs(form, rails.requiredInputSelector),
+ nonBlankFileInputs = rails.nonBlankInputs(form, rails.fileInputSelector);
+
+ if (!rails.allowAction(form)) return rails.stopEverything(e);
+
+ // skip other logic when required values are missing or file upload is present
+ if (blankRequiredInputs && form.attr("novalidate") == undefined && rails.fire(form, 'ajax:aborted:required', [blankRequiredInputs])) {
+ return rails.stopEverything(e);
+ }
+
+ if (remote) {
+ if (nonBlankFileInputs) {
+ return rails.fire(form, 'ajax:aborted:file', [nonBlankFileInputs]);
+ }
+
+ // If browser does not support submit bubbling, then this live-binding will be called before direct
+ // bindings. Therefore, we should directly call any direct bindings before remotely submitting form.
+ if (!$.support.submitBubbles && $().jquery < '1.7' && rails.callFormSubmitBindings(form, e) === false) return rails.stopEverything(e);
+
+ rails.handleRemote(form);
+ return false;
+
+ } else {
+ // slight timeout so that the submit button gets properly serialized
+ setTimeout(function(){ rails.disableFormElements(form); }, 13);
+ }
+ });
+
+ $(document).delegate(rails.formInputClickSelector, 'click.rails', function(event) {
+ var button = $(this);
+
+ if (!rails.allowAction(button)) return rails.stopEverything(event);
+
+ // register the pressed submit button
+ var name = button.attr('name'),
+ data = name ? {name:name, value:button.val()} : null;
+
+ button.closest('form').data('ujs:submit-button', data);
+ });
+
+ $(document).delegate(rails.formSubmitSelector, 'ajax:beforeSend.rails', function(event) {
+ if (this == event.target) rails.disableFormElements($(this));
+ });
+
+ $(document).delegate(rails.formSubmitSelector, 'ajax:complete.rails', function(event) {
+ if (this == event.target) rails.enableFormElements($(this));
+ });
+
+})( jQuery );
13 lib/templates/erb/scaffold/_form.html.erb
View
@@ -0,0 +1,13 @@
+<%%= simple_form_for(@<%= singular_table_name %>) do |f| %>
+ <%%= f.error_notification %>
+
+ <div class="form-inputs">
+ <%- attributes.each do |attribute| -%>
+ <%%= f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %> %>
+ <%- end -%>
+ </div>
+
+ <div class="form-actions">
+ <%%= f.button :submit %>
+ </div>
+<%% end %>
2  spec/factories/users.rb
View
@@ -1,6 +1,6 @@
FactoryGirl.define do
factory :user do
- name 'alice'
+ username 'alice'
password 'hushhush'
password_confirmation {|u| u.password}
end
18 spec/models/user_spec.rb
View
@@ -4,9 +4,9 @@
describe '.new' do
let(:user) { User.new }
- context 'with a name, password and password_confirmation' do
+ context 'with a username, password and password_confirmation' do
before do
- user.name = 'alice'
+ user.username = 'alice'
user.password = 'hushhush'
user.password_confirmation = 'hushhush'
end
@@ -20,9 +20,9 @@
end
end
- context 'with a name only' do
+ context 'with a username only' do
before do
- user.name = 'alice'
+ user.username = 'alice'
end
it 'should not be valid' do
@@ -30,9 +30,9 @@
end
end
- context 'with a name, password and wrong password_confirmation' do
+ context 'with a username, password and wrong password_confirmation' do
before do
- user.name = 'alice'
+ user.username = 'alice'
user.password = 'hushhush'
user.password_confirmation = 'nopenope'
end
@@ -43,10 +43,10 @@
end
context 'when mass assigned whitelisted attributes' do
- let(:user) { User.new(:name => 'alice', :password => 'hushhush', :password_confirmation => 'hushhush') }
+ let(:user) { User.new(:username => 'alice', :password => 'hushhush', :password_confirmation => 'hushhush') }
it 'those attributes should be set' do
- user.name.should == 'alice'
+ user.username.should == 'alice'
user.password.should == 'hushhush'
user.password_confirmation.should == 'hushhush'
end
@@ -62,7 +62,7 @@
describe '.authenticate' do
before do
@alice = Factory(:user)
- @bob = Factory(:user, :name => 'bob', :password => 'password')
+ @bob = Factory(:user, :username => 'bob', :password => 'password')
end
let(:alice) { @alice }
Please sign in to comment.
Something went wrong with that request. Please try again.