mongosteen 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a2c5e9a3b1fb54ca264fbc34e957dc6ca0b8a0d8
4
+ data.tar.gz: f2be921ba553aaf9ab72f9da5d62606ca48c7fc6
5
+ SHA512:
6
+ metadata.gz: 80f24df9d54d67e47e1c0ec5fb0ac3bb8b4e7f2edf232870cca933c2028efdf657d5cd2930fa30c9717689faf2bc7271d72bcb04171e7ce4a68a04e1b0e533ba
7
+ data.tar.gz: 8b4a72708186e3f209acd208de62a5eba5df49f4059a996de049a43fa436eeb086d74b447cd69e50e1795f42522b13a575ead23a611e753cfe7070812dbdb59a
@@ -0,0 +1,24 @@
1
+ We love pull requests. Here’s a quick guide:
2
+
3
+ 1. Fork the repository.
4
+ 2. Make your changes in a topic branch.
5
+ 3. Squash your commits into a single one (more on that [here](http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html)).
6
+ 4. Rebase against `origin/master`, push to your fork and submit a pull request.
7
+ 5. If you are writing a new feature please add documentation for it by making another pull request to the `gh-pages` branch.
8
+
9
+ At this point you’re waiting on us. We like to at least comment on, if not
10
+ accept, pull requests within three business days (and, typically, one business
11
+ day). We may suggest some changes or improvements or alternatives.
12
+
13
+ Some things that will increase the chance that your pull request is accepted:
14
+
15
+ * Fix a bug, refactor code or expand an existing feature.
16
+ * Use the right syntax and naming conventions.
17
+ * Update parts of the documentation that are affected by your contribution.
18
+
19
+ **Git Commit Messages**
20
+
21
+ * Capitalize your commit messages.
22
+ * Start your message with a verb.
23
+ * Use present tense.
24
+ * Refer to the issue/PR number in your squashed commit message.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright © 2015 [Slate Studio, LLC.](http://slatestudio.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the “Software”), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,28 @@
1
+ ![Mongosteen](https://slate-git-images.s3-us-west-1.amazonaws.com/mongosteen.png)
2
+
3
+ # Mongosteen
4
+
5
+ ## An easy way to add restful actions
6
+
7
+ Mongosteen is a library that helps to easily add restful actions to mongoid models with support of search, pagination, scopes, history, json config.
8
+
9
+ - search
10
+ - pagination
11
+ - scopes
12
+ - json config
13
+ - history
14
+
15
+ ## The Mongosteen family
16
+
17
+ - [Character](https://github.com/slate-studio/chr): A simple and lightweight library for building data management web apps
18
+ - [Character Admin](https://github.com/slate-studio/chr-admin): Flexible javascript admin solution for Rails applications
19
+
20
+ ## Credits
21
+
22
+ [![Slate Studio](https://slate-git-images.s3-us-west-1.amazonaws.com/slate.png)](http://slatestudio.com)
23
+
24
+ Mongosteen is maintained and funded by [Slate Studio, LLC](http://slatestudio.com). Tweet your questions or suggestions to [@slatestudio](https://twitter.com/slatestudio) and while you’re at it follow us too.
25
+
26
+ ## License
27
+
28
+ Copyright © 2015 [Slate Studio, LLC](http://slatestudio.com). Mongosteen is free software, and may be redistributed under the terms specified in the [license](LICENSE.md).
@@ -0,0 +1,3 @@
1
+ # encoding: utf-8
2
+ require "rubygems"
3
+ require "bundler"
@@ -0,0 +1,24 @@
1
+ # NOTE: this is required by sorted_relations.rb
2
+ module Mongoid
3
+ class FakeCriteria
4
+ def initialize(documents)
5
+ @documents = documents
6
+ end
7
+
8
+ def limit(quantity)
9
+ Mongoid::FakeCriteria.new(@documents[0..quantity-1])
10
+ end
11
+
12
+ def raw
13
+ @documents
14
+ end
15
+
16
+ def as_json(options={})
17
+ @documents.as_json(options)
18
+ end
19
+
20
+ def method_missing(*args, &block)
21
+ @documents.send(*args, &block)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,19 @@
1
+ module Mongoid
2
+ module SerializableId
3
+ extend ActiveSupport::Concern
4
+
5
+ def serializable_hash(options={})
6
+ attrs = super(options)
7
+ attrs.each_pair do |k, v|
8
+ if k.end_with?('_id')
9
+ attrs[k] = v.to_s
10
+ end
11
+
12
+ if k.end_with?('_ids')
13
+ attrs[k] = attrs[k].collect { |id| id.to_s }
14
+ end
15
+ end
16
+ attrs
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,61 @@
1
+ # NOTE: this is an updated version of mongoid-sorted-relations gem
2
+ # https://github.com/demarque/mongoid-sorted-relations
3
+
4
+ # NOTE: the problem is that mongoid returnes object in random order
5
+ # for relation queries, this patch stores relations ids order
6
+ # and sort objects using cached order.
7
+
8
+ module Mongoid
9
+ module SortedRelations
10
+ extend ActiveSupport::Concern
11
+
12
+ included do
13
+ after_initialize :define_sorted_methods
14
+ after_initialize :cache_relations_ids_order
15
+ after_save :cache_relations_ids_order
16
+ end
17
+
18
+ def define_sorted_methods
19
+ self.relations.each do |key, relation|
20
+ if [:has_many, :has_and_belongs_to_many].include? relation.macro
21
+ self.class.send(:define_method, "sorted_#{relation.name}") do
22
+ sorted_relation(relation)
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ def cache_relations_ids_order
29
+ @__sorted_relation_criterias = {}
30
+ @__sorted_relation_ids = {}
31
+
32
+ self.relations.each do |key, relation|
33
+ if [:has_many, :has_and_belongs_to_many].include? relation.macro
34
+ relation_ids = self.send(relation.key)
35
+
36
+ @__sorted_relation_ids[relation.key] = [ relation_ids ].flatten.map do |rid|
37
+ rid.to_s
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ def sorted_relation(relation)
44
+ @__sorted_relation_criterias[relation.name] ||= sorted_related_criteria(relation)
45
+ end
46
+
47
+ def sorted_related_criteria(relation)
48
+ related_documents = self.send(relation.name)
49
+
50
+ sorted_documents = related_documents.sort_by do |x|
51
+ @__sorted_relation_ids[relation.key].index(x.id.to_s)
52
+ end
53
+
54
+ return Mongoid::FakeCriteria.new(sorted_documents)
55
+ end
56
+ end
57
+ end
58
+
59
+
60
+
61
+
@@ -0,0 +1,48 @@
1
+ # CodeKit needs relative paths
2
+ dir = File.dirname(__FILE__)
3
+ $LOAD_PATH.unshift dir unless $LOAD_PATH.include?(dir)
4
+
5
+ require 'inherited_resources'
6
+ require 'has_scope'
7
+ require 'kaminari'
8
+
9
+ module Mongosteen
10
+ autoload :Actions, 'mongosteen/actions'
11
+ autoload :ClassMethods, 'mongosteen/class_methods'
12
+ autoload :BaseHelpers, 'mongosteen/base_helpers'
13
+ autoload :PermittedParams, 'mongosteen/permitted_params'
14
+
15
+ class Engine < ::Rails::Engine
16
+ require 'mongosteen/engine'
17
+ require 'mongoid/fake_criteria'
18
+ require 'mongoid/sorted_relations'
19
+ require 'mongoid/serializable_id'
20
+ end
21
+ end
22
+
23
+ class ActionController::Base
24
+ # You can call reactor_resources in your controller to have
25
+ # all the required modules and funcionality included.
26
+ def self.reactor_resources
27
+ self.class_eval do
28
+ inherit_resources
29
+
30
+ respond_to :json
31
+ class_attribute :as_json_config
32
+
33
+ extend Mongosteen::ClassMethods
34
+ include Mongosteen::BaseHelpers
35
+ include Mongosteen::Actions
36
+ include Mongosteen::PermittedParams
37
+
38
+ # configure permitted_params to accept all attributes
39
+ instance_name = self.resources_configuration[:self][:instance_name]
40
+ define_method("#{ instance_name }_params") { params_all_permitted }
41
+ private "#{ instance_name }_params"
42
+ end
43
+ end
44
+ end
45
+
46
+
47
+
48
+
@@ -0,0 +1,37 @@
1
+ module Mongosteen
2
+ module Actions
3
+ # GET /resources
4
+ def index(options={}, &block)
5
+ super do |success, failure|
6
+ success.json { render json: get_collection_ivar.as_json(as_json_config) }
7
+ end
8
+ end
9
+
10
+ # GET /resources/1
11
+ def show(options={}, &block)
12
+ super do |success, failure|
13
+ success.json { render json: get_resource_version.as_json(as_json_config) }
14
+ end
15
+ end
16
+
17
+ # POST /resources
18
+ def create(options={}, &block)
19
+ super do |success, failure|
20
+ success.json { render json: get_resource_ivar.as_json(as_json_config) }
21
+ failure.json { render json: get_resource_ivar.errors, status: :unprocessable_entity }
22
+ end
23
+ end
24
+
25
+ # PUT /resources/1
26
+ def update(options={}, &block)
27
+ super do |success, failure|
28
+ success.json { render json: get_resource_ivar.as_json(as_json_config) }
29
+ failure.json { render json: get_resource_ivar.errors, status: :unprocessable_entity }
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+
36
+
37
+
@@ -0,0 +1,59 @@
1
+ # List of dependencies:
2
+ # - inherited_resources
3
+ # - has_scope
4
+ # - mongoid_search
5
+ # - kaminari
6
+ # - mongoid-history
7
+
8
+ module Mongosteen
9
+ module BaseHelpers
10
+ protected
11
+ # add support for scopes, search and pagination
12
+ def collection
13
+ get_collection_ivar || begin
14
+ chain = end_of_association_chain
15
+
16
+ # scopes
17
+ chain = apply_scopes(chain)
18
+
19
+ # search
20
+ if params[:search]
21
+ chain = chain.search(params[:search].to_s.downcase, match: :all)
22
+ end
23
+
24
+ # pagination
25
+ if params[:page]
26
+ per_page = params[:perPage] || 20
27
+ chain = chain.page(params[:page]).per(per_page)
28
+ else
29
+ chain = chain.all
30
+ end
31
+
32
+ set_collection_ivar(chain)
33
+ end
34
+ end
35
+
36
+ # add support for history
37
+ def get_resource_version
38
+ resource = get_resource_ivar
39
+
40
+ version = params[:version].try(:to_i)
41
+
42
+ if version && version > 0 && version < resource.version
43
+ resource.undo(nil, from: version + 1, to: resource.version)
44
+ resource.version = version
45
+ end
46
+
47
+ return resource
48
+ end
49
+
50
+ # support for json resource configuration
51
+ def as_json_config
52
+ self.class.as_json_config
53
+ end
54
+ end
55
+ end
56
+
57
+
58
+
59
+
@@ -0,0 +1,7 @@
1
+ module Mongosteen
2
+ module ClassMethods
3
+ def json_config(config_hash)
4
+ self.as_json_config = config_hash
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module Mongosteen
2
+ class Engine < Rails::Engine
3
+ # auto wire
4
+ end
5
+ end
@@ -0,0 +1,46 @@
1
+ module Mongosteen
2
+ module PermittedParams
3
+ # Permits all parameters that are sent, unless `project_params`
4
+ # (or `smth_else_params`) defined, which is a default Rails
5
+ # controller method for strong parameters definition.
6
+ 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
41
+ end
42
+ end
43
+ end
44
+
45
+
46
+
@@ -0,0 +1,3 @@
1
+ module Mongosteen
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'mongosteen/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'mongosteen'
7
+ s.version = Mongosteen::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['Alexander Kravets']
10
+ s.email = 'alex@slatestudio.com'
11
+ s.license = 'MIT'
12
+ s.homepage = 'https://github.com/slate-studio/mongosteen'
13
+ s.summary = 'An easy way to add restful actions'
14
+ s.description = <<-DESC
15
+ Mongosteen is a library that helps to easily add restful actions to
16
+ mongoid models with support of search, pagination, scopes, history,
17
+ json config.
18
+ DESC
19
+
20
+ s.rubyforge_project = 'mongosteen'
21
+
22
+ s.files = `git ls-files`.split("\n")
23
+ s.require_paths = ['lib']
24
+
25
+ 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
+ s.add_dependency('mongoid_search', '~> 0.3') # search
29
+ s.add_dependency('has_scope', '~> 0.6') # scopes
30
+ s.add_dependency('mongoid-history', '~> 0.4') # history
31
+ end
32
+
33
+
34
+
35
+
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongosteen
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Kravets
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mongoid
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: inherited_resources
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: kaminari
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.16'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mongoid_search
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.3'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: has_scope
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.6'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: mongoid-history
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.4'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.4'
97
+ description: |
98
+ Mongosteen is a library that helps to easily add restful actions to
99
+ mongoid models with support of search, pagination, scopes, history,
100
+ json config.
101
+ email: alex@slatestudio.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - CONTRIBUTING.md
107
+ - Gemfile
108
+ - LICENSE.md
109
+ - README.md
110
+ - Rakefile
111
+ - lib/mongoid/fake_criteria.rb
112
+ - lib/mongoid/serializable_id.rb
113
+ - lib/mongoid/sorted_relations.rb
114
+ - lib/mongosteen.rb
115
+ - lib/mongosteen/actions.rb
116
+ - lib/mongosteen/base_helpers.rb
117
+ - lib/mongosteen/class_methods.rb
118
+ - lib/mongosteen/engine.rb
119
+ - lib/mongosteen/permitted_params.rb
120
+ - lib/mongosteen/version.rb
121
+ - mongosteen.gemspec
122
+ homepage: https://github.com/slate-studio/mongosteen
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project: mongosteen
142
+ rubygems_version: 2.4.5
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: An easy way to add restful actions
146
+ test_files: []