mongosteen 0.1.3 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/.DS_Store +0 -0
  3. data/.gitignore +3 -0
  4. data/Gemfile +8 -0
  5. data/Gemfile.lock +204 -0
  6. data/README.md +32 -55
  7. data/Rakefile +16 -2
  8. data/lib/mongosteen.rb +24 -13
  9. data/lib/mongosteen/actions.rb +6 -0
  10. data/lib/mongosteen/base_helpers.rb +6 -7
  11. data/lib/mongosteen/class_methods.rb +6 -2
  12. data/lib/mongosteen/permitted_params.rb +3 -34
  13. data/lib/mongosteen/version.rb +1 -1
  14. data/mongosteen.gemspec +13 -4
  15. data/test/.DS_Store +0 -0
  16. data/test/controllers/.keep +0 -0
  17. data/test/helpers/.keep +0 -0
  18. data/test/integration/api_test.rb +95 -0
  19. data/test/rails_app/.DS_Store +0 -0
  20. data/test/rails_app/Rakefile +6 -0
  21. data/test/rails_app/app/assets/images/.keep +0 -0
  22. data/test/rails_app/app/assets/javascripts/application.js +16 -0
  23. data/test/rails_app/app/assets/stylesheets/application.css +15 -0
  24. data/test/rails_app/app/controllers/application_controller.rb +5 -0
  25. data/test/rails_app/app/controllers/concerns/.keep +0 -0
  26. data/test/rails_app/app/controllers/test_items_controller.rb +5 -0
  27. data/test/rails_app/app/controllers/test_permit_items_controller.rb +9 -0
  28. data/test/rails_app/app/helpers/application_helper.rb +2 -0
  29. data/test/rails_app/app/mailers/.keep +0 -0
  30. data/test/rails_app/app/models/concerns/.keep +0 -0
  31. data/test/rails_app/app/models/test_item.rb +16 -0
  32. data/test/rails_app/app/models/test_permit_item.rb +7 -0
  33. data/test/rails_app/app/views/layouts/application.html.erb +14 -0
  34. data/test/rails_app/bin/bundle +3 -0
  35. data/test/rails_app/bin/rails +8 -0
  36. data/test/rails_app/bin/rake +8 -0
  37. data/test/rails_app/bin/setup +29 -0
  38. data/test/rails_app/bin/spring +15 -0
  39. data/test/rails_app/config.ru +4 -0
  40. data/test/rails_app/config/application.rb +32 -0
  41. data/test/rails_app/config/boot.rb +3 -0
  42. data/test/rails_app/config/environment.rb +5 -0
  43. data/test/rails_app/config/environments/development.rb +38 -0
  44. data/test/rails_app/config/environments/production.rb +76 -0
  45. data/test/rails_app/config/environments/test.rb +42 -0
  46. data/test/rails_app/config/initializers/assets.rb +11 -0
  47. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  48. data/test/rails_app/config/initializers/cookies_serializer.rb +3 -0
  49. data/test/rails_app/config/initializers/filter_parameter_logging.rb +4 -0
  50. data/test/rails_app/config/initializers/inflections.rb +16 -0
  51. data/test/rails_app/config/initializers/mime_types.rb +4 -0
  52. data/test/rails_app/config/initializers/session_store.rb +3 -0
  53. data/test/rails_app/config/initializers/wrap_parameters.rb +9 -0
  54. data/test/rails_app/config/locales/en.yml +23 -0
  55. data/test/rails_app/config/mongoid.yml +9 -0
  56. data/test/rails_app/config/routes.rb +6 -0
  57. data/test/rails_app/config/secrets.yml +22 -0
  58. data/test/rails_app/db/seeds.rb +7 -0
  59. data/test/rails_app/lib/assets/.keep +0 -0
  60. data/test/rails_app/lib/tasks/.keep +0 -0
  61. data/test/rails_app/public/404.html +67 -0
  62. data/test/rails_app/public/422.html +67 -0
  63. data/test/rails_app/public/500.html +66 -0
  64. data/test/rails_app/public/favicon.ico +0 -0
  65. data/test/rails_app/public/robots.txt +5 -0
  66. data/test/rails_app/vendor/assets/javascripts/.keep +0 -0
  67. data/test/rails_app/vendor/assets/stylesheets/.keep +0 -0
  68. data/test/test_helper.rb +16 -0
  69. metadata +158 -22
  70. data/CONTRIBUTING.md +0 -24
  71. data/lib/mongoid/fake_criteria.rb +0 -24
  72. data/lib/mongoid/serializable_id.rb +0 -19
  73. data/lib/mongoid/sorted_relations.rb +0 -61
  74. data/lib/mongosteen/engine.rb +0 -5
@@ -1,5 +1,6 @@
1
1
  module Mongosteen
2
2
  module Actions
3
+
3
4
  # GET /resources
4
5
  def index(options={}, &block)
5
6
  super do |success, failure|
@@ -7,6 +8,7 @@ module Mongosteen
7
8
  end
8
9
  end
9
10
 
11
+
10
12
  # GET /resources/1
11
13
  def show(options={}, &block)
12
14
  super do |success, failure|
@@ -14,6 +16,7 @@ module Mongosteen
14
16
  end
15
17
  end
16
18
 
19
+
17
20
  # POST /resources
18
21
  def create(options={}, &block)
19
22
  super do |success, failure|
@@ -22,6 +25,7 @@ module Mongosteen
22
25
  end
23
26
  end
24
27
 
28
+
25
29
  # PUT /resources/1
26
30
  def update(options={}, &block)
27
31
  super do |success, failure|
@@ -29,6 +33,8 @@ module Mongosteen
29
33
  failure.json { render json: get_resource_ivar.errors, status: :unprocessable_entity }
30
34
  end
31
35
  end
36
+
37
+
32
38
  end
33
39
  end
34
40
 
@@ -1,13 +1,8 @@
1
- # List of dependencies:
2
- # - inherited_resources
3
- # - has_scope
4
- # - mongoid_search
5
- # - kaminari
6
- # - mongoid-history
7
-
8
1
  module Mongosteen
9
2
  module BaseHelpers
3
+
10
4
  protected
5
+
11
6
  # add support for scopes, search and pagination
12
7
  def collection
13
8
  get_collection_ivar || begin
@@ -33,6 +28,7 @@ module Mongosteen
33
28
  end
34
29
  end
35
30
 
31
+
36
32
  # add support for history
37
33
  def get_resource_version
38
34
  resource = get_resource_ivar
@@ -47,10 +43,13 @@ module Mongosteen
47
43
  return resource
48
44
  end
49
45
 
46
+
50
47
  # support for json resource configuration
51
48
  def as_json_config
52
49
  self.class.as_json_config
53
50
  end
51
+
52
+
54
53
  end
55
54
  end
56
55
 
@@ -1,7 +1,11 @@
1
1
  module Mongosteen
2
2
  module ClassMethods
3
- def json_config(config_hash)
3
+
4
+ def json_config(config_hash={})
5
+ config_hash[:methods] ||= []
6
+ config_hash[:methods].concat(json_default_methods).uniq!
4
7
  self.as_json_config = config_hash
5
8
  end
9
+
6
10
  end
7
- end
11
+ end
@@ -1,46 +1,15 @@
1
1
  module Mongosteen
2
2
  module PermittedParams
3
+
3
4
  # Permits all parameters that are sent, unless `project_params`
4
5
  # (or `smth_else_params`) defined, which is a default Rails
5
6
  # controller method for strong parameters definition.
6
7
  def params_all_permitted
7
- permit_fields = []
8
-
9
- params[resource_request_name].each do |key, value|
10
- permit_fields << attr_name_or_map(key, value)
11
- end
12
-
13
- return params.require(resource_request_name).permit(permit_fields)
14
- end
15
-
16
- # Allow all sent in params attributes to be written
17
- def attr_name_or_map(attr_name, val)
18
- # NOTE: RECURSION is used to map all hashes in params to update nested documents
19
- if val.is_a?(Hash)
20
- map = {} ; map[attr_name] = []
21
-
22
- if val.first[0] == val.first[0].to_i.to_s # check if key is an integer which means we have an array of nested objects
23
-
24
- #val.each do |nestedObjectKey, nestedObjectValue|
25
- # nestedObjectValue.each { |arr_value_key, arr_value_value| map[attr_name] << attr_name_or_map(arr_value_key, arr_value_value) }
26
- #end
27
-
28
- val.first[1].each { |arr_value_key, arr_value_value| map[attr_name] << attr_name_or_map(arr_value_key, arr_value_value) }
29
-
30
- else
31
- val.each { |hsh_key, hsh_value| map[attr_name] << attr_name_or_map(hsh_key, hsh_value) }
32
- end
33
-
34
- return map
35
- elsif val.is_a?(Array)
36
- map = {} ; map[attr_name] = []
37
- return map
38
- else
39
- return attr_name
40
- end
8
+ return params.require(resource_request_name).permit!
41
9
  end
42
10
  end
43
11
  end
44
12
 
45
13
 
46
14
 
15
+
@@ -1,3 +1,3 @@
1
1
  module Mongosteen
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.7"
3
3
  end
@@ -12,7 +12,7 @@ Gem::Specification.new do |s|
12
12
  s.homepage = 'https://github.com/slate-studio/mongosteen'
13
13
  s.summary = 'An easy way to add restful actions'
14
14
  s.description = <<-DESC
15
- Mongosteen is a library that helps to easily add restful actions to
15
+ Mongosteen is a library that helps to easily add RESTful actions to
16
16
  mongoid models with support of search, pagination, scopes, history,
17
17
  json config.
18
18
  DESC
@@ -22,12 +22,21 @@ json config.
22
22
  s.files = `git ls-files`.split("\n")
23
23
  s.require_paths = ['lib']
24
24
 
25
+ s.add_dependency('inherited_resources', '>= 1.6') # resources
26
+ s.add_dependency('mongoid-history', '>= 0.4.5') # history
25
27
  s.add_dependency('mongoid', '>= 4.0') # orm
26
- s.add_dependency('inherited_resources', '>= 1.6') # base actions
27
- s.add_dependency('kaminari', '>= 0.16') # pagination
28
28
  s.add_dependency('mongoid_search', '>= 0.3') # search
29
+ s.add_dependency('kaminari', '>= 0.16') # pagination
29
30
  s.add_dependency('has_scope', '> 0.5') # scopes
30
- s.add_dependency('mongoid-history', '>= 0.4.5') # history
31
+
32
+ s.add_development_dependency('rake', [ '>= 0.8.7' ])
33
+
34
+ # automated tests
35
+ s.add_development_dependency 'rails', '~> 4.1.2'
36
+ s.add_development_dependency 'database_cleaner'
37
+ s.add_development_dependency 'factory_girl_rails'
38
+ s.add_development_dependency 'minitest-reporters'
39
+ s.add_development_dependency 'minitest-focus'
31
40
  end
32
41
 
33
42
 
Binary file
File without changes
File without changes
@@ -0,0 +1,95 @@
1
+ require 'test_helper'
2
+
3
+ class ApiTest < Capybara::Rails::TestCase
4
+ test 'create test item' do
5
+ page.driver.post('/test_items', { test_item: { title: 'Test title', description: 'Test description'} } )
6
+
7
+ assert_equal(1, TestItem.count)
8
+ assert_equal('Test title', TestItem.first.title)
9
+ assert_equal('Test description', TestItem.first.description)
10
+ end
11
+
12
+ test 'update test item' do
13
+ TestItem.create!(title: 'Test title', description: 'Test description')
14
+
15
+ page.driver.put("/test_items/#{TestItem.first.id}", { test_item: { title: 'Test title 2', description: 'Test description 2'} })
16
+ assert_equal('Test title 2', TestItem.first.title)
17
+ assert_equal('Test description 2', TestItem.first.description)
18
+ end
19
+
20
+ test 'show test item' do
21
+ TestItem.create!(title: 'Test title', description: 'Test description')
22
+
23
+ visit "/test_items/#{TestItem.first.id}.json"
24
+ test_item_hash = JSON.parse(page.body)
25
+ assert_equal('Test title', test_item_hash["title"])
26
+ assert_equal('Test description', test_item_hash["description"])
27
+ end
28
+
29
+ test 'index test item' do
30
+ TestItem.create!(title: 'Test title', description: 'Test description')
31
+ TestItem.create!(title: 'Test title 2', description: 'Test description 2')
32
+
33
+ visit '/test_items.json'
34
+
35
+ test_item_array = JSON.parse(page.body)
36
+
37
+ assert_equal('Test title', test_item_array[0]["title"])
38
+ assert_equal('Test description', test_item_array[0]["description"])
39
+
40
+ assert_equal('Test title 2', test_item_array[1]["title"])
41
+ assert_equal('Test description 2', test_item_array[1]["description"])
42
+ end
43
+
44
+ test 'pagination' do
45
+ (0..2).each do |index|
46
+ TestItem.create!(title: "Test title #{index}", description: "Test description #{index}")
47
+ end
48
+
49
+ visit '/test_items.json?perPage=2&page=1'
50
+
51
+ test_item_array = JSON.parse(page.body)
52
+ assert_equal(2, test_item_array.length)
53
+ end
54
+
55
+ test 'search' do
56
+ (0..2).each do |index|
57
+ TestItem.create!(title: "Test title #{index}", description: "Test description #{index}")
58
+ end
59
+
60
+ TestItem.create!(title: "Mongosteen", description: "Mongosteen description")
61
+
62
+ visit '/test_items.json?search=Mongosteen'
63
+
64
+ test_item_array = JSON.parse(page.body)
65
+ assert_equal(1, test_item_array.length)
66
+ assert_equal('Mongosteen', test_item_array[0]['title'])
67
+ end
68
+
69
+ test 'scope' do
70
+ TestItem.create!(title: "Test title", description: "Test description")
71
+ TestItem.create!(title: "Test title 2", description: "Test description 2")
72
+
73
+ visit '/test_items.json?test_scope=true'
74
+
75
+ test_item_array = JSON.parse(page.body)
76
+ assert_equal(1, test_item_array.length)
77
+ assert_equal('Test title', test_item_array[0]['title'])
78
+ end
79
+
80
+ test 'test method' do
81
+ TestItem.create!(title: "Test title", description: "Test description")
82
+
83
+ visit '/test_items.json'
84
+
85
+ test_item_array = JSON.parse(page.body)
86
+ assert_equal('Test method', test_item_array[0]['test_method'])
87
+ end
88
+
89
+ test 'test permit params' do
90
+ page.driver.post('/test_permit_items', { test_permit_item: { title: 'Test title', description: 'Test description'} } )
91
+
92
+ assert_equal('Test title', TestPermitItem.first.title)
93
+ assert_equal(nil, TestPermitItem.first.description)
94
+ end
95
+ end
Binary file
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
File without changes
@@ -0,0 +1,16 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require turbolinks
16
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,5 @@
1
+ class TestItemsController < ApplicationController
2
+ mongosteen
3
+ has_scope :test_scope, type: :boolean
4
+ json_config methods: [ :test_method ]
5
+ end
@@ -0,0 +1,9 @@
1
+ class TestPermitItemsController < ApplicationController
2
+ mongosteen
3
+
4
+ private
5
+
6
+ def test_permit_item_params
7
+ params.require(:test_permit_item).permit(:title)
8
+ end
9
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
File without changes
@@ -0,0 +1,16 @@
1
+ class TestItem
2
+ include Mongoid::Document
3
+ include Mongoid::Search
4
+
5
+ # attributes
6
+ field :title, type: String
7
+ field :description, type: String
8
+
9
+ scope :test_scope, -> { where title: "Test title" }
10
+
11
+ search_in :title, :description
12
+
13
+ def test_method
14
+ 'Test method'
15
+ end
16
+ end
@@ -0,0 +1,7 @@
1
+ class TestPermitItem
2
+ include Mongoid::Document
3
+
4
+ # attributes
5
+ field :title, type: String
6
+ field :description, type: String
7
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>RailsApp</title>
5
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
6
+ <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ begin
3
+ load File.expand_path("../spring", __FILE__)
4
+ rescue LoadError
5
+ end
6
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
7
+ require_relative '../config/boot'
8
+ require 'rails/commands'
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ begin
3
+ load File.expand_path("../spring", __FILE__)
4
+ rescue LoadError
5
+ end
6
+ require_relative '../config/boot'
7
+ require 'rake'
8
+ Rake.application.run
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ require 'pathname'
3
+
4
+ # path to your application root.
5
+ APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
6
+
7
+ Dir.chdir APP_ROOT do
8
+ # This script is a starting point to setup your application.
9
+ # Add necessary setup steps to this file:
10
+
11
+ puts "== Installing dependencies =="
12
+ system "gem install bundler --conservative"
13
+ system "bundle check || bundle install"
14
+
15
+ # puts "\n== Copying sample files =="
16
+ # unless File.exist?("config/database.yml")
17
+ # system "cp config/database.yml.sample config/database.yml"
18
+ # end
19
+
20
+ puts "\n== Preparing database =="
21
+ system "bin/rake db:setup"
22
+
23
+ puts "\n== Removing old logs and tempfiles =="
24
+ system "rm -f log/*"
25
+ system "rm -rf tmp/cache"
26
+
27
+ puts "\n== Restarting application server =="
28
+ system "touch tmp/restart.txt"
29
+ end