bento 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. data/README.rdoc +62 -42
  2. data/lib/bento/controllers/account_scopable.rb +31 -2
  3. data/lib/bento/controllers/helpers.rb +13 -9
  4. data/lib/bento/models/account.rb +18 -37
  5. data/lib/bento/models/modules/trial.rb +15 -0
  6. data/lib/bento/models/modules/user_accessors.rb +26 -0
  7. data/lib/bento/models/modules/user_association.rb +13 -0
  8. data/lib/bento/models/modules/validations.rb +14 -0
  9. data/lib/bento/rails/routes.rb +24 -0
  10. data/lib/bento/rails.rb +2 -0
  11. data/lib/bento/version.rb +1 -1
  12. data/lib/generators/active_record/bento_generator.rb +1 -1
  13. data/lib/generators/active_record/templates/add_migration.rb +2 -2
  14. data/lib/generators/active_record/templates/create_migration.rb +4 -5
  15. data/lib/generators/bento/bento_generator.rb +4 -0
  16. data/lib/generators/bento/orm_helpers.rb +1 -1
  17. data/spec/bento/controllers/helpers_spec.rb +24 -0
  18. data/spec/bento/models/account_spec.rb +5 -95
  19. data/spec/bento/models/modules/trial_spec.rb +22 -0
  20. data/spec/bento/models/modules/user_accessors_spec.rb +48 -0
  21. data/spec/bento/models/modules/user_association_spec.rb +16 -0
  22. data/spec/bento/models/modules/validations_spec.rb +27 -0
  23. data/spec/bento/rails/routes_spec.rb +27 -0
  24. data/spec/bento_spec.rb +4 -0
  25. data/spec/controllers/bento_for_routes_spec.rb +55 -0
  26. data/spec/rails_app/app/controllers/custom_accounts_controller.rb +7 -1
  27. data/spec/rails_app/app/models/account.rb +1 -1
  28. data/spec/rails_app/app/models/site.rb +8 -0
  29. data/spec/rails_app/app/models/user.rb +1 -0
  30. data/spec/rails_app/app/views/custom_accounts/index.html.erb +14 -0
  31. data/spec/rails_app/app/views/custom_accounts/new.html.erb +11 -7
  32. data/spec/rails_app/app/views/custom_accounts/show.html.erb +12 -0
  33. data/spec/rails_app/app/views/layouts/application.html.erb +3 -2
  34. data/spec/rails_app/app/views/projects/_all_projects.html.erb +1 -0
  35. data/spec/rails_app/app/views/projects/show.html.erb +1 -0
  36. data/spec/rails_app/config/routes.rb +6 -1
  37. data/spec/rails_app/db/development.sqlite3 +0 -0
  38. data/spec/rails_app/db/migrate/20101029122256_bento_create_sites.rb +14 -0
  39. data/spec/rails_app/db/migrate/20101029122257_bento_add_site_id_to_sites.rb +9 -0
  40. data/spec/rails_app/db/schema.rb +10 -1
  41. data/spec/rails_app/db/test.sqlite3 +0 -0
  42. data/spec/rails_app/log/development.log +50 -1220
  43. data/spec/rails_app/{db/production.sqlite3 → log/production.log} +0 -0
  44. data/spec/rails_app/log/server.log +0 -0
  45. data/spec/rails_app/log/test.log +32539 -21983
  46. data/spec/spec_helper.rb +1 -1
  47. data/spec/support/blueprints.rb +4 -0
  48. metadata +24 -6
  49. data/spec/rails_app/tmp/pids/server.pid +0 -1
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ class UserAccessorsTestAccount < ActiveRecord::Base
4
+ set_table_name("accounts")
5
+ attr_accessible :name, :plan, :first_name, :last_name, :email, :password_confirmation, :password, :created_at
6
+ bento(:user_accessors)
7
+ end
8
+
9
+ describe Bento::Models::Modules::UserAccessors do
10
+ let(:account_params) do
11
+ { :name => "Hashrocket", :first_name => "Obie", :last_name => "Fernandez", :email => "obie@hashrocket.com", :password => "test1234" }
12
+ end
13
+
14
+ subject { UserAccessorsTestAccount.new }
15
+ it { should_not be_invalid_without(:name) }
16
+ it { should have_user_accessors }
17
+ it { should respond_to(:users) }
18
+ it { should_not respond_to(:trial_days_remaining) }
19
+ it { should respond_to(:build_user) }
20
+
21
+ describe ".build_user" do
22
+ context "all user attributes are blank" do
23
+ it "creates the account without the user" do
24
+ account = UserAccessorsTestAccount.new(:name => "Elabs")
25
+ account.save.should be_true
26
+ User.find_by_account_id(account.id).should be_nil
27
+ end
28
+ end
29
+
30
+ context "at least one user attributes is present" do
31
+ context "successfully creating a user" do
32
+ subject do
33
+ account = UserAccessorsTestAccount.create!(account_params)
34
+ account.users.first
35
+ end
36
+
37
+ its(:email) { should == "obie@hashrocket.com" }
38
+ its(:first_name) { should == "Obie" }
39
+ its(:last_name) { should == "Fernandez" }
40
+ end
41
+
42
+ context "unsuccessfully creating a user" do
43
+ subject { UserAccessorsTestAccount.new(account_params.merge(:email => "")).tap(&:valid?) }
44
+ it { subject.errors.should include(:email) }
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ class UserAssociationTestAccount < ActiveRecord::Base
4
+ set_table_name("accounts")
5
+ attr_accessible :name, :created_at
6
+ bento(:user_association)
7
+ end
8
+
9
+ describe Bento::Models::Modules::Trial do
10
+ subject { UserAssociationTestAccount.new }
11
+ it { should_not be_invalid_without(:name) }
12
+ it { should_not have_user_accessors }
13
+ it { should respond_to(:users) }
14
+ it { should_not respond_to(:trial_days_remaining) }
15
+ it { should_not respond_to(:build_user) }
16
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ class ValidationsTestAccount < ActiveRecord::Base
4
+ set_table_name("accounts")
5
+ attr_accessible :name
6
+ bento(:validations)
7
+ end
8
+
9
+ describe Bento::Models::Modules::Validations do
10
+ subject { ValidationsTestAccount.new }
11
+ it { should be_invalid_without(:name) }
12
+ it { should_not have_user_accessors }
13
+ it { should_not respond_to(:users) }
14
+ it { should_not respond_to(:trial_days_remaining) }
15
+ it { should_not respond_to(:build_user) }
16
+
17
+ it "validates pressence of name" do
18
+ account2 = ValidationsTestAccount.create(:name => "")
19
+ account2.should have(1).error_on(:name)
20
+ end
21
+
22
+ it "validates uniquness of name" do
23
+ account1 = ValidationsTestAccount.create!(:name => "Elabs")
24
+ account2 = ValidationsTestAccount.create(:name => "Elabs")
25
+ account2.should have(1).error_on(:name)
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActionDispatch::Routing::Mapper do
4
+ subject { ActionDispatch::Routing::RouteSet.new }
5
+
6
+ describe "#bento_for" do
7
+ context "when given one resource name" do
8
+ before { Bento::Controllers::Helpers.should_receive(:define_helpers).with(:account) }
9
+
10
+ it "defines the current_<resource_name>-helper when given a string" do
11
+ subject.draw { bento_for("account") }
12
+ end
13
+
14
+ it "defines the current_<resource_name>-helper a symbol" do
15
+ subject.draw { bento_for(:account) }
16
+ end
17
+ end
18
+
19
+ context "when given several resource names" do
20
+ it "defines the current_<resource_name>-helper symbols" do
21
+ Bento::Controllers::Helpers.should_receive(:define_helpers).with(:account)
22
+ Bento::Controllers::Helpers.should_receive(:define_helpers).with(:site)
23
+ subject.draw { bento_for(:account, :site) }
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bento do
4
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ class SitesController < ActionController::Base
4
+ def show; render(:text => "s"); end
5
+ def index; render(:text => "i"); end
6
+ def new; render(:text => "n"); end
7
+ def edit; render(:text => "e"); end
8
+ def create; redirect_to sites_url; end
9
+ def update; redirect_to sites_url; end
10
+ def destroy; redirect_to sites_url; end
11
+ end
12
+
13
+ context "with the default bento controller" do
14
+ describe Bento::AccountsController do
15
+ before { controller.stubs(:authenticate_user!) }
16
+ let(:account) { Account.make }
17
+
18
+ describe "#bento_for" do
19
+ it "defines resource routes" do
20
+ with_routing do |map|
21
+ map.draw { bento_for :accounts }
22
+
23
+ get(:show, :id => account.id); response.should be_success
24
+ get(:index); response.should be_success
25
+ get(:sign_up); response.should be_success
26
+ get(:new, :id => account.id); response.should be_success
27
+ get(:edit, :id => account.id); response.should be_success
28
+ post(:create); response.should be_redirect
29
+ put(:update, :id => account.id); response.should be_redirect
30
+ put(:destroy, :id => account.id); response.should be_redirect
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ context "with a custonly defined controller" do
38
+ describe SitesController do
39
+ describe "#bento_for" do
40
+ it "defines resource routes" do
41
+ with_routing do |map|
42
+ map.draw { bento_for :sites }
43
+
44
+ get(:show, :id => 1); response.should be_success
45
+ get(:index); response.should be_success
46
+ get(:new, :id => 1); response.should be_success
47
+ get(:edit, :id => 1); response.should be_success
48
+ post(:create); response.should be_redirect
49
+ put(:update, :id => 1); response.should be_redirect
50
+ put(:destroy, :id => 1); response.should be_redirect
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -1,9 +1,15 @@
1
1
  class CustomAccountsController < Bento::AccountsController
2
- defaults :resource_class => Account, :collection_name => 'accounts', :instance_name => 'account'
2
+ defaults :resource_class => Site, :collection_name => 'sites', :instance_name => 'site'
3
3
  skip_before_filter :authenticate_user!, :only => %w[new]
4
4
 
5
5
  def new
6
6
  flash[:special] = "Welcome to '#{controller_path}' controller!"
7
7
  super
8
8
  end
9
+
10
+ protected
11
+
12
+ def after_create_url
13
+ custom_account_path(resource)
14
+ end
9
15
  end
@@ -1,7 +1,7 @@
1
1
  class Account < ActiveRecord::Base
2
2
  # Include all bento modules. Others available are:
3
3
  # :all, :validations, :user_accessors, :user_association, :user_accessors, :trial
4
- bento_account
4
+ bento :all
5
5
 
6
6
  # Setup accessible (or protected) attributes for your model
7
7
  attr_accessible :name, :plan, :first_name, :last_name, :email, :password_confirmation, :password
@@ -0,0 +1,8 @@
1
+ class Site < ActiveRecord::Base
2
+ # Include all bento modules. Others available are:
3
+ # :all, :validations, :user_accessors, :user_association, :user_accessors, :trial
4
+ bento :all
5
+
6
+ # Setup accessible (or protected) attributes for your model
7
+ attr_accessible :name, :first_name, :last_name, :email, :password_confirmation, :password
8
+ end
@@ -5,4 +5,5 @@ class User < ActiveRecord::Base
5
5
 
6
6
  attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :account
7
7
  belongs_to :account
8
+ belongs_to :site
8
9
  end
@@ -0,0 +1,14 @@
1
+ <h1>Custom accounts</h1>
2
+ <h2>Current site: <%= current_site.name %></h2>
3
+
4
+ <%= table_for Site, @sites do |table| %>
5
+ <%= table.head :show, :edit, :delete %>
6
+ <%= table.body do |row| %>
7
+ <%= row.cell link_to(row.record.name, account_path(row.record)) %>
8
+ <%= row.cell link_to("Edit '#{row.record.name}'", edit_custom_account_path(row.record)) %>
9
+ <%= row.cell button_to("Delete '#{row.record.name}'", custom_account_path(row.record), :method => :delete) %>
10
+ <% end %>
11
+ <%= table.foot do %>
12
+ <%= link_to "Add account", new_custom_account_path %>
13
+ <% end %>
14
+ <% end %>
@@ -1,10 +1,14 @@
1
1
  <h1>New account</h1>
2
- <%= simple_form_for @account do |form| -%>
3
- <%= form.input :name, :label => 'Account name', :hint => 'For example, your own name or the name of your company.' %>
4
- <%= form.input :first_name %>
5
- <%= form.input :last_name %>
6
- <%= form.input :email, :label => 'Email address', :hint => 'Will be used as your login.' %>
7
- <%= form.input :password %>
8
- <%= form.input :password_confirmation %>
2
+ <%= simple_form_for @site, :url => custom_accounts_path do |form| -%>
3
+ <% if params[:sign_up] %>
4
+ <%= form.input :name, :label => 'Account name' %>
5
+ <%= form.input :first_name %>
6
+ <%= form.input :last_name %>
7
+ <%= form.input :email, :label => 'Email address' %>
8
+ <%= form.input :password %>
9
+ <%= form.input :password_confirmation %>
10
+ <% else %>
11
+ <%= form.input :name %>
12
+ <% end %>
9
13
  <%= form.button :submit, "Create account", :class => "Submit" %>
10
14
  <% end -%>
@@ -0,0 +1,12 @@
1
+ <h1><%= @site.name %> (Custom account)</h1>
2
+
3
+ <%= show_for @site do |show| %>
4
+ <%= show.attributes :name %>
5
+ <% end %>
6
+
7
+ <p>
8
+ <%= link_to("Edit", edit_account_path(@site)) %>
9
+ <%= button_to('Delete', account_path(@site), :method => :delete) %>
10
+ </p>
11
+
12
+ <p><%= link_to "Back to accounts", accounts_path %></p>
@@ -11,8 +11,9 @@
11
11
  <% end %>
12
12
 
13
13
  <%= link_to "Manage accounts", accounts_path %>
14
- <%= link_to "Bento Accounts", new_account_path %>
15
- <%= link_to "Custom Accounts", new_custom_account_path %>
14
+ <%= link_to "Bento accounts", new_account_path %>
15
+ <%= link_to "Custom accounts", new_custom_account_path(:sign_up => true) %>
16
+ <%= link_to "Manage custom accounts", custom_accounts_path %>
16
17
 
17
18
  <% if user_signed_in? %>
18
19
  <span>Current account: <%= current_account.name %></span>
@@ -2,6 +2,7 @@
2
2
  <ul>
3
3
  <% Project.all.each do |project| %>
4
4
  <li>
5
+ <%= link_to project.name, scoping_account_project_path(project.account, project) %>
5
6
  <%= link_to "Edit '#{project.name}'", edit_all_project_path(project) %>
6
7
  <%= button_to "Delete '#{project.name}'", project_path(project), :method => :delete %>
7
8
  </li>
@@ -0,0 +1 @@
1
+ <h1><%= @project.name %></h1>
@@ -1,9 +1,14 @@
1
1
  RailsApp::Application.routes.draw do
2
2
  devise_for :users
3
+ bento_for :accounts, :sites
3
4
 
4
- resources :custom_accounts, :only => %w[new create]
5
+ resources :custom_accounts
5
6
  resources :all_projects, :only => %w[edit]
6
7
  resources :projects
7
8
 
9
+ resources :scoping_accounts do
10
+ resources :projects
11
+ end
12
+
8
13
  root :to => "home#index"
9
14
  end
Binary file
@@ -0,0 +1,14 @@
1
+ class BentoCreateSites < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :sites do |t|
4
+ t.string :name
5
+ t.timestamps
6
+ end
7
+
8
+ add_index :sites, :name, :unique => true
9
+ end
10
+
11
+ def self.down
12
+ drop_table :sites
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ class BentoAddSiteIdToSites < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :users, :site_id, :integer
4
+ end
5
+
6
+ def self.down
7
+ remove_column :users, :column_name
8
+ end
9
+ end
@@ -10,7 +10,7 @@
10
10
  #
11
11
  # It's strongly recommended to check this file into your version control system.
12
12
 
13
- ActiveRecord::Schema.define(:version => 20101015143011) do
13
+ ActiveRecord::Schema.define(:version => 20101029122257) do
14
14
 
15
15
  create_table "accounts", :force => true do |t|
16
16
  t.string "name"
@@ -28,6 +28,14 @@ ActiveRecord::Schema.define(:version => 20101015143011) do
28
28
  t.datetime "updated_at"
29
29
  end
30
30
 
31
+ create_table "sites", :force => true do |t|
32
+ t.string "name"
33
+ t.datetime "created_at"
34
+ t.datetime "updated_at"
35
+ end
36
+
37
+ add_index "sites", ["name"], :name => "index_sites_on_name", :unique => true
38
+
31
39
  create_table "users", :force => true do |t|
32
40
  t.string "email", :default => "", :null => false
33
41
  t.string "encrypted_password", :limit => 128, :default => "", :null => false
@@ -37,6 +45,7 @@ ActiveRecord::Schema.define(:version => 20101015143011) do
37
45
  t.datetime "created_at"
38
46
  t.datetime "updated_at"
39
47
  t.integer "account_id"
48
+ t.integer "site_id"
40
49
  end
41
50
 
42
51
  add_index "users", ["email"], :name => "index_users_on_email", :unique => true
Binary file