administer 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. data/README.rdoc +3 -4
  2. data/app/controllers/administer/application_controller.rb +12 -0
  3. data/app/controllers/administer/entities_controller.rb +76 -0
  4. data/app/helpers/administer/dashboard_helper.rb +1 -1
  5. data/app/helpers/administer/entities_helper.rb +2 -0
  6. data/app/views/administer/dashboard/index.html.haml +1 -1
  7. data/app/views/administer/entities/_form.haml +9 -0
  8. data/app/views/administer/entities/edit.html.haml +9 -0
  9. data/app/views/administer/entities/index.html.haml +21 -0
  10. data/app/views/administer/entities/new.haml +9 -0
  11. data/app/views/administer/fields/_belongs_to.html.haml +2 -0
  12. data/app/views/administer/fields/_date_select.html.haml +2 -0
  13. data/app/views/administer/fields/_has_many.html.haml +4 -0
  14. data/app/views/administer/fields/_has_one.html.haml +2 -0
  15. data/app/views/administer/fields/_text_area.html.haml +2 -0
  16. data/app/views/administer/fields/_text_field.html.haml +2 -0
  17. data/app/views/administer/shared/_errors.html.haml +6 -0
  18. data/app/views/layouts/administer.html.haml +21 -0
  19. data/config/routes.rb +4 -4
  20. data/lib/administer.rb +1 -0
  21. data/lib/administer/active_record.rb +22 -0
  22. data/lib/administer/config.rb +37 -0
  23. data/lib/administer/config/controller_config.rb +24 -0
  24. data/lib/administer/config/model_config.rb +23 -0
  25. data/lib/administer/engine.rb +5 -0
  26. data/lib/administer/fields.rb +34 -0
  27. data/lib/administer/fields/association.rb +21 -0
  28. data/lib/administer/fields/base.rb +15 -0
  29. data/lib/administer/fields/belongs_to.rb +12 -0
  30. data/lib/administer/fields/date_select.rb +6 -0
  31. data/lib/administer/fields/field_builder.rb +25 -0
  32. data/lib/administer/fields/has_many.rb +22 -0
  33. data/lib/administer/fields/has_one.rb +24 -0
  34. data/lib/administer/fields/text_area.rb +6 -0
  35. data/lib/administer/fields/text_field.rb +6 -0
  36. data/lib/administer/model.rb +40 -13
  37. data/spec/controllers/administer/entities_controller_spec.rb +23 -0
  38. data/spec/{rails_root/spec/controllers → controllers}/posts_controller_spec.rb +1 -1
  39. data/spec/factories/categories.rb +3 -0
  40. data/spec/factories/comments.rb +3 -0
  41. data/spec/{rails_root/spec/factories → factories}/posts.rb +0 -0
  42. data/spec/lib/config/model_config_spec.rb +35 -0
  43. data/spec/lib/config_spec.rb +29 -0
  44. data/spec/lib/fields/field_builder_spec.rb +10 -0
  45. data/spec/{rails_root/spec/models/administer → lib}/model_spec.rb +5 -4
  46. data/spec/models/comment_spec.rb +5 -0
  47. data/spec/rails_root/Gemfile +2 -0
  48. data/spec/rails_root/Gemfile.lock +150 -0
  49. data/spec/rails_root/README +256 -0
  50. data/spec/rails_root/Rakefile +7 -0
  51. data/spec/rails_root/app/controllers/application_controller.rb +4 -0
  52. data/spec/rails_root/app/models/attachment.rb +3 -0
  53. data/spec/rails_root/app/models/category.rb +1 -0
  54. data/spec/rails_root/app/models/comment.rb +3 -0
  55. data/spec/rails_root/app/models/post.rb +8 -0
  56. data/spec/rails_root/app/views/layouts/application.html.erb +14 -0
  57. data/spec/rails_root/app/views/posts/_form.html.haml +19 -0
  58. data/spec/rails_root/app/views/posts/edit.html.haml +5 -0
  59. data/spec/rails_root/app/views/posts/index.html.haml +10 -0
  60. data/spec/rails_root/app/views/posts/new.html.haml +3 -0
  61. data/spec/rails_root/app/views/posts/show.html.haml +10 -0
  62. data/spec/rails_root/config.ru +4 -0
  63. data/spec/rails_root/config/cucumber.yml +8 -0
  64. data/spec/rails_root/config/database.yml +25 -0
  65. data/spec/rails_root/config/initializers/administer.rb +23 -0
  66. data/spec/rails_root/config/locales/en.yml +5 -0
  67. data/spec/rails_root/db/development.sqlite3 +0 -0
  68. data/spec/rails_root/db/migrate/20110104100347_add_publish_on_to_post.rb +9 -0
  69. data/spec/rails_root/db/migrate/20110214190645_add_category_to_post.rb +9 -0
  70. data/spec/rails_root/db/migrate/20110609184125_create_comments.rb +13 -0
  71. data/spec/rails_root/db/migrate/20110628215407_create_attachments.rb +13 -0
  72. data/spec/rails_root/db/schema.rb +44 -0
  73. data/spec/rails_root/db/test.sqlite3 +0 -0
  74. data/spec/rails_root/features/administer/dashboard.feature +15 -0
  75. data/spec/rails_root/features/administer/posts.feature +72 -0
  76. data/spec/rails_root/features/listing_posts.feature +15 -0
  77. data/spec/rails_root/features/managing_posts.feature +70 -0
  78. data/spec/rails_root/features/step_definitions/categories_steps.rb +6 -0
  79. data/spec/rails_root/features/step_definitions/comments_steps.rb +6 -0
  80. data/spec/rails_root/features/step_definitions/post_steps.rb +21 -0
  81. data/spec/rails_root/features/step_definitions/web_steps.rb +5 -2
  82. data/spec/rails_root/features/support/env.rb +32 -46
  83. data/spec/rails_root/features/support/factory_girl.rb +2 -0
  84. data/spec/rails_root/features/support/matchers.rb +55 -0
  85. data/spec/rails_root/features/support/paths.rb +2 -0
  86. data/spec/rails_root/features/support/selectors.rb +39 -0
  87. data/spec/rails_root/lib/tasks/cucumber.rake +65 -0
  88. data/spec/rails_root/log/development.log +19507 -0
  89. data/spec/rails_root/log/test.log +45042 -0
  90. data/spec/rails_root/public/404.html +26 -0
  91. data/spec/rails_root/public/422.html +26 -0
  92. data/spec/rails_root/public/500.html +26 -0
  93. data/spec/rails_root/public/favicon.ico +0 -0
  94. data/spec/rails_root/public/images/rails.png +0 -0
  95. data/spec/rails_root/public/javascripts/application.js +2 -0
  96. data/spec/rails_root/public/javascripts/controls.js +965 -0
  97. data/spec/rails_root/public/javascripts/dragdrop.js +974 -0
  98. data/spec/rails_root/public/javascripts/effects.js +1123 -0
  99. data/spec/rails_root/public/javascripts/prototype.js +6001 -0
  100. data/spec/rails_root/public/javascripts/rails.js +175 -0
  101. data/spec/rails_root/public/robots.txt +5 -0
  102. data/spec/rails_root/public/stylesheets/scaffold.css +60 -0
  103. data/spec/rails_root/script/cucumber +10 -0
  104. data/spec/rails_root/script/rails +6 -0
  105. data/spec/rails_root/spec/models/attachment_spec.rb +5 -0
  106. data/spec/rails_root/tmp/capybara/capybara-20101221001337.html +43 -0
  107. data/spec/rails_root/tmp/capybara/capybara-20101221001509.html +47 -0
  108. data/spec/rails_root/tmp/capybara/capybara-20110104162405.html +110 -0
  109. data/spec/rails_root/tmp/capybara/capybara-20110214224604.html +51 -0
  110. data/spec/rails_root/tmp/capybara/capybara-20110214225336.html +51 -0
  111. data/spec/rails_root/tmp/capybara/capybara-20110214225545.html +51 -0
  112. data/spec/{rails_root/spec/routing → routing}/posts_routing_spec.rb +1 -1
  113. data/spec/{rails_root/spec/spec_helper.rb → spec_helper.rb} +6 -4
  114. data/spec/{rails_root/spec/views → views}/posts/edit.html.haml_spec.rb +1 -1
  115. data/spec/{rails_root/spec/views → views}/posts/new.html.haml_spec.rb +1 -1
  116. data/spec/{rails_root/spec/views → views}/posts/show.html.haml_spec.rb +1 -1
  117. metadata +306 -46
  118. data/app/controllers/administer/models_controller.rb +0 -16
  119. data/app/views/administer/models/index.html.haml +0 -14
  120. data/spec/rails_root/spec/controllers/administer/models_controller_spec.rb +0 -19
@@ -0,0 +1,12 @@
1
+ module Administer
2
+ module Fields
3
+ class BelongsTo < Administer::Fields::Association
4
+ attr_reader :primary_key
5
+
6
+ def initialize(name, primary_key, association_class, parent)
7
+ super(name, association_class, parent)
8
+ @primary_key = primary_key
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ module Administer
2
+ module Fields
3
+ class DateSelect < Administer::Fields::Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,25 @@
1
+ module Administer
2
+ module Fields
3
+ class FieldBuilder
4
+ @@registered_classes = SuperclassHash.new
5
+ class << self
6
+ def register_class(klass, &block)
7
+ @@registered_classes[klass] = block
8
+ end
9
+ end
10
+
11
+ def build_fields_for(fields)
12
+ fields.map do |field|
13
+ field_for(field)
14
+ end
15
+ end
16
+
17
+ def field_for(object)
18
+ block = @@registered_classes[object.class]
19
+ raise "FieldBuilder has no class registered #{object.class}" unless block
20
+ block.call(object)
21
+ end
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,22 @@
1
+ module Administer
2
+ module Fields
3
+ class HasMany < Administer::Fields::Association
4
+ def checked?(parent, object)
5
+ parent.send(name).include?(object)
6
+ end
7
+
8
+ def check_box_name
9
+ "#{parent_class.name.downcase}[#{relation_ids}][]"
10
+ end
11
+
12
+ def dom_id(object)
13
+ "#{parent_class.name.downcase}_#{relation_ids}_#{object.id}"
14
+ end
15
+
16
+ private
17
+ def relation_ids
18
+ "#{name.to_s.singularize}_ids"
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ module Administer
2
+ module Fields
3
+ class HasOne < Administer::Fields::Association
4
+ attr_reader :foreign_key
5
+
6
+ def initialize(name, foreign_key, association_class, parent)
7
+ super(name, association_class, parent)
8
+ @foreign_key = foreign_key
9
+ end
10
+
11
+ def select_name
12
+ "#{parent_class.name.downcase}[#{foreign_key}]"
13
+ end
14
+
15
+ def dom_id
16
+ "#{parent_class.name.downcase}_#{foreign_key}"
17
+ end
18
+
19
+ def options
20
+ [["", nil]] + collection.map{ |object| [display_object(object), object.id] }
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,6 @@
1
+ module Administer
2
+ module Fields
3
+ class TextArea < Administer::Fields::Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Administer
2
+ module Fields
3
+ class TextField < Administer::Fields::Base
4
+ end
5
+ end
6
+ end
@@ -1,22 +1,49 @@
1
+ require 'administer/fields'
2
+
1
3
  module Administer
2
4
  class Model
3
-
4
5
  @@models = []
5
-
6
- def self.all
7
- if @@models.empty?
8
- Dir[Rails.root.join('app/models/**/*.rb')].each do |path|
9
- # TODO: Is it better to get model name from its class definition within model file?
10
- model_name = path.split('/').last.gsub(/\.rb$/, '')
11
- @@models << Administer::Model.lookup(model_name)
6
+
7
+ class << self
8
+ def all
9
+ if @@models.empty?
10
+ Rails.application.paths.app.models.paths.each do |models_path|
11
+ Dir[File.expand_path("**/*.rb", models_path)].each do |path|
12
+ model_name = path.split('/').last.gsub(/\.rb$/, '')
13
+ @@models << Administer::Model.for(model_name)
14
+ end
15
+ end
12
16
  end
17
+ @@models
18
+ end
19
+
20
+ def for(model_name)
21
+ klass = model_name.is_a?(Class) ? model_name : lookup(model_name)
22
+ Model.new(klass)
23
+ end
24
+
25
+ private
26
+ def lookup(model_name)
27
+ model_name.camelize.constantize
13
28
  end
14
- @@models
15
29
  end
16
-
17
- def self.lookup(model_name)
18
- model_name.camelize.constantize
30
+
31
+ attr_accessor :entity
32
+
33
+ def initialize(model_class)
34
+ @entity = model_class
35
+ @config = Administer::Config.for(@entity)
36
+ end
37
+
38
+ def display_name(object)
39
+ display_function = @config.display_name
40
+ if display_function.is_a? Symbol
41
+ return object.send(display_function)
42
+ else
43
+ return object.instance_eval(&display_function)
44
+ end
19
45
  end
20
-
46
+
47
+ include Administer::Fields
21
48
  end
22
49
  end
@@ -0,0 +1,23 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Administer::EntitiesController do
4
+ describe "GET /index" do
5
+ it "should lookup for proper collection" do
6
+ controller.should_receive(:model_class).and_return(Post)
7
+ posts = mock_model(Post)
8
+ Post.should_receive(:all).and_return(posts)
9
+ get :index, :model_name => 'post'
10
+ assigns[:collection].should == posts
11
+ end
12
+ end
13
+
14
+ describe "DELETE /:id" do
15
+ it "should display error message when unable to delete" do
16
+ controller.should_receive(:model_class).twice.and_return(Post)
17
+ post = mock_model(Post, :id => 1, :destroy => false)
18
+ Post.should_receive(:find).and_return(post)
19
+ delete :destroy, :model_name => 'post', :id => 1
20
+ flash[:error].should == "Could not destroy object."
21
+ end
22
+ end
23
+ end
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe PostsController do
4
4
 
@@ -0,0 +1,3 @@
1
+ Factory.define :category do |c|
2
+ c.name "My Thoughts"
3
+ end
@@ -0,0 +1,3 @@
1
+ Factory.define :comment do |c|
2
+ c.body "The comment"
3
+ end
@@ -0,0 +1,35 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '../../../spec_helper')
2
+
3
+ describe Administer::Config::ModelConfigBuilder do
4
+ before do
5
+ @model_config = mock("model_config")
6
+ Administer::Config::ModelConfig.stub(:new => @model_config)
7
+ end
8
+
9
+ describe :display_name do
10
+ it "should be possible to define display_name as block" do
11
+ config_proc = proc{}
12
+ @model_config.should_receive(:display_name=).with(config_proc)
13
+
14
+ Administer::Config::ModelConfigBuilder.new do
15
+ display_name &config_proc
16
+ end
17
+ end
18
+
19
+ it "should be possible to define display_name as symbol" do
20
+ @model_config.should_receive(:display_name=).with(:method)
21
+
22
+ Administer::Config::ModelConfigBuilder.new do
23
+ display_name :method
24
+ end
25
+ end
26
+
27
+ it "should raise an exception if both block and metod_name is provided" do
28
+ lambda{
29
+ Administer::Config::ModelConfigBuilder.new do
30
+ display_name :method do end
31
+ end
32
+ }.should raise_error
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,29 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Administer::Config do
4
+ describe :configure do
5
+ it "should assign class variable @@config" do
6
+ Administer::Config.should_receive(:new).and_return("the config")
7
+ Administer::Config.configure
8
+ Administer::Config.class_variable_get(:@@config).should == "the config"
9
+ end
10
+
11
+ describe "after defining configuration for particular model" do
12
+ before(:each) do
13
+ Administer::Config.configure do
14
+ model Post do
15
+ display_name :title
16
+ end
17
+ end
18
+ end
19
+
20
+ it "should be possible to get this configuration" do
21
+ Administer::Config.for(Post).should_not be_nil
22
+ end
23
+
24
+ it "should have defined values" do
25
+ Administer::Config.for(Post).display_name.should == :title
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,10 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Administer::Fields::FieldBuilder do
4
+ it "you can register class that would get converted into field" do
5
+ Administer::Fields::FieldBuilder.register_class ActiveRecord::ConnectionAdapters::Column do
6
+ TextField
7
+ end
8
+ Administer::Fields::FieldBuilder.class_variable_get(:@@registered_classes)[ActiveRecord::ConnectionAdapters::Column].should_not be_nil
9
+ end
10
+ end
@@ -1,15 +1,16 @@
1
- require 'spec_helper'
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe Administer::Model do
4
4
  it "should lookup for model given its name" do
5
- Administer::Model.lookup('post').should == Post
5
+ Administer::Model.for('post').entity.should == Post
6
6
  end
7
-
7
+
8
8
  it "should return all models defined within ROOT/app/models/, using lookup" do
9
+ pending "test for cases when @@models is empty and not empty"
9
10
  Rails.root.should_receive(:join).with('app/models/**/*.rb').and_return('models_path')
10
11
  Dir.should_receive(:[]).with('models_path').and_return(['ROOT/app/models/post.rb'])
11
12
  Administer::Model.should_receive(:lookup).with('post').and_return('PostStub')
12
13
  Administer::Model.all.should == ['PostStub']
13
14
  end
14
-
15
+
15
16
  end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe Comment do
4
+ pending "add some examples to (or delete) #{__FILE__}"
5
+ end
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec :path => "../../"
@@ -0,0 +1,150 @@
1
+ PATH
2
+ remote: /Users/piotrj/Projects/administer
3
+ specs:
4
+ administer (0.2.2)
5
+ haml (>= 3.0.18)
6
+ rails (~> 3.0.3)
7
+ superclass_hash
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ ZenTest (4.5.0)
13
+ abstract (1.0.0)
14
+ actionmailer (3.0.8)
15
+ actionpack (= 3.0.8)
16
+ mail (~> 2.2.19)
17
+ actionpack (3.0.8)
18
+ activemodel (= 3.0.8)
19
+ activesupport (= 3.0.8)
20
+ builder (~> 2.1.2)
21
+ erubis (~> 2.6.6)
22
+ i18n (~> 0.5.0)
23
+ rack (~> 1.2.1)
24
+ rack-mount (~> 0.6.14)
25
+ rack-test (~> 0.5.7)
26
+ tzinfo (~> 0.3.23)
27
+ activemodel (3.0.8)
28
+ activesupport (= 3.0.8)
29
+ builder (~> 2.1.2)
30
+ i18n (~> 0.5.0)
31
+ activerecord (3.0.8)
32
+ activemodel (= 3.0.8)
33
+ activesupport (= 3.0.8)
34
+ arel (~> 2.0.10)
35
+ tzinfo (~> 0.3.23)
36
+ activeresource (3.0.8)
37
+ activemodel (= 3.0.8)
38
+ activesupport (= 3.0.8)
39
+ activesupport (3.0.8)
40
+ arel (2.0.10)
41
+ autotest (4.4.6)
42
+ ZenTest (>= 4.4.1)
43
+ builder (2.1.2)
44
+ capybara (1.0.0.rc1)
45
+ mime-types (>= 1.16)
46
+ nokogiri (>= 1.3.3)
47
+ rack (>= 1.0.0)
48
+ rack-test (>= 0.5.4)
49
+ selenium-webdriver (~> 0.2.0)
50
+ xpath (~> 0.1.4)
51
+ childprocess (0.1.9)
52
+ ffi (~> 1.0.6)
53
+ configuration (1.2.0)
54
+ cucumber (0.10.6)
55
+ builder (>= 2.1.2)
56
+ diff-lcs (>= 1.1.2)
57
+ gherkin (~> 2.4.0)
58
+ json (>= 1.4.6)
59
+ term-ansicolor (>= 1.0.5)
60
+ cucumber-rails (0.5.2)
61
+ capybara (>= 1.0.0.rc1)
62
+ cucumber (>= 0.10.5)
63
+ nokogiri (>= 1.4.4)
64
+ rack-test (>= 0.5.7)
65
+ database_cleaner (0.6.7)
66
+ diff-lcs (1.1.2)
67
+ erubis (2.6.6)
68
+ abstract (>= 1.0.0)
69
+ factory_girl (1.3.3)
70
+ ffi (1.0.9)
71
+ gherkin (2.4.0)
72
+ json (>= 1.4.6)
73
+ haml (3.1.2)
74
+ i18n (0.5.0)
75
+ json (1.5.1)
76
+ json_pure (1.5.1)
77
+ launchy (0.4.0)
78
+ configuration (>= 0.0.5)
79
+ rake (>= 0.8.1)
80
+ mail (2.2.19)
81
+ activesupport (>= 2.3.6)
82
+ i18n (>= 0.4.0)
83
+ mime-types (~> 1.16)
84
+ treetop (~> 1.4.8)
85
+ mime-types (1.16)
86
+ nokogiri (1.4.4)
87
+ polyglot (0.3.1)
88
+ rack (1.2.3)
89
+ rack-mount (0.6.14)
90
+ rack (>= 1.0.0)
91
+ rack-test (0.5.7)
92
+ rack (>= 1.0)
93
+ rails (3.0.8)
94
+ actionmailer (= 3.0.8)
95
+ actionpack (= 3.0.8)
96
+ activerecord (= 3.0.8)
97
+ activeresource (= 3.0.8)
98
+ activesupport (= 3.0.8)
99
+ bundler (~> 1.0)
100
+ railties (= 3.0.8)
101
+ railties (3.0.8)
102
+ actionpack (= 3.0.8)
103
+ activesupport (= 3.0.8)
104
+ rake (>= 0.8.7)
105
+ thor (~> 0.14.4)
106
+ rake (0.9.2)
107
+ rspec (2.6.0)
108
+ rspec-core (~> 2.6.0)
109
+ rspec-expectations (~> 2.6.0)
110
+ rspec-mocks (~> 2.6.0)
111
+ rspec-core (2.6.4)
112
+ rspec-expectations (2.6.0)
113
+ diff-lcs (~> 1.1.2)
114
+ rspec-mocks (2.6.0)
115
+ rspec-rails (2.6.1)
116
+ actionpack (~> 3.0)
117
+ activesupport (~> 3.0)
118
+ railties (~> 3.0)
119
+ rspec (~> 2.6.0)
120
+ rubyzip (0.9.4)
121
+ selenium-webdriver (0.2.1)
122
+ childprocess (>= 0.1.7)
123
+ ffi (>= 1.0.7)
124
+ json_pure
125
+ rubyzip
126
+ sqlite3 (1.3.3)
127
+ superclass_hash (0.0.1)
128
+ term-ansicolor (1.0.5)
129
+ thor (0.14.6)
130
+ treetop (1.4.9)
131
+ polyglot (>= 0.3.1)
132
+ tzinfo (0.3.27)
133
+ xpath (0.1.4)
134
+ nokogiri (~> 1.3)
135
+
136
+ PLATFORMS
137
+ ruby
138
+
139
+ DEPENDENCIES
140
+ administer!
141
+ autotest
142
+ capybara (>= 0.4.1)
143
+ cucumber (>= 0.10.6)
144
+ cucumber-rails (>= 0.5.2)
145
+ database_cleaner
146
+ factory_girl
147
+ launchy
148
+ nokogiri
149
+ rspec-rails (>= 2.1.0)
150
+ sqlite3