greenin 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rspec +3 -0
- data/.travis.yml +11 -0
- data/Gemfile +4 -0
- data/README.md +32 -11
- data/greenin.gemspec +14 -2
- data/lib/active_record_overrides.rb +19 -10
- data/lib/greenin/version.rb +1 -1
- data/spec/dummy/README.rdoc +28 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/api/base_api.rb +10 -0
- data/spec/dummy/app/api/dummy_api.rb +2 -0
- data/spec/dummy/app/assets/images/.keep +0 -0
- data/spec/dummy/app/assets/javascripts/application.js +13 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/controllers/application_controller.rb +5 -0
- data/spec/dummy/app/controllers/concerns/.keep +0 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/mailers/.keep +0 -0
- data/spec/dummy/app/models/.keep +0 -0
- data/spec/dummy/app/models/concerns/.keep +0 -0
- data/spec/dummy/app/models/post.rb +6 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +29 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +43 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +38 -0
- data/spec/dummy/config/environments/production.rb +79 -0
- data/spec/dummy/config/environments/test.rb +37 -0
- data/spec/dummy/config/initializers/assets.rb +11 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/initializer.rb +4 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +57 -0
- data/spec/dummy/config/secrets.yml +22 -0
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/migrate/20150224174059_create_posts.rb +9 -0
- data/spec/dummy/db/schema.rb +22 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/lib/assets/.keep +0 -0
- data/spec/dummy/log/.keep +0 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/factories/posts.rb +5 -0
- data/spec/models/active_record_spec.rb +18 -0
- data/spec/models/api_spec.rb +50 -0
- data/spec/models/settings_spec.rb +6 -0
- data/spec/rails_helper.rb +65 -0
- data/spec/spec_helper.rb +95 -0
- metadata +247 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2033e9cdfa4ed544139dc352a45fdda77afba6af
|
4
|
+
data.tar.gz: 4dcd42334e6957f04ecec0e8567dc2a5e9c93484
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 05afeec084aff3a535804d795c9c0a300c08a7a9c183b3a3a9738dd0247774835d06da58f396ec2458deef939112c46c3d40f15e5d46216d6a4a5f6fd9f27f86
|
7
|
+
data.tar.gz: ff27306b5c8a98516660cc72030f53d53a21c132ab13ba3c2a03774bfd7202200d36e6911e9d813f4899fc313aee02c0f4c0456c9eea49c6d00991364ec2dc2a
|
data/.gitignore
CHANGED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# Greenin
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/greenin.svg)](http://badge.fury.io/rb/greenin)
|
3
|
+
[![Build Status](https://travis-ci.org/itbeaver/greenin.svg?branch=develop)](https://travis-ci.org/itbeaver/greenin)
|
2
4
|
[![Code Climate](https://codeclimate.com/github/itbeaver/greenin/badges/gpa.svg)](https://codeclimate.com/github/itbeaver/greenin)
|
5
|
+
[![Test Coverage](https://codeclimate.com/github/itbeaver/greenin/badges/coverage.svg)](https://codeclimate.com/github/itbeaver/greenin)
|
6
|
+
[![Dependency Status](https://gemnasium.com/itbeaver/greenin.svg)](https://gemnasium.com/itbeaver/greenin)
|
3
7
|
|
4
8
|
Describe your Grape Entities in Rails Active Record models
|
5
9
|
|
@@ -57,21 +61,38 @@ class Post < ActiveRecord::Base
|
|
57
61
|
end
|
58
62
|
```
|
59
63
|
|
60
|
-
|
64
|
+
This gem add methods to your ActiveRecord models that call Entity.represent(self, args), so, you can call entity like that:
|
61
65
|
```ruby
|
62
|
-
Post::Entity
|
63
|
-
# =>
|
64
|
-
|
65
|
-
Post.entity
|
66
|
-
# => Post::Entity
|
66
|
+
Post::Entity == Post.entity
|
67
|
+
# => true
|
67
68
|
|
68
69
|
Post.all.entity
|
69
|
-
# => #<Post::Entity:
|
70
|
-
#
|
70
|
+
# => {"posts"=>[#<Post::Entity:0x007ff0880d7d18
|
71
|
+
# @object=#<Post id: 1, title: "Some title", created_at: "2015-02-27 17:48:07",
|
72
|
+
# updated_at: "2015-02-27 17:48:07">, @options={:collection=>true}>, ...]}
|
73
|
+
|
74
|
+
Post.all.entity.to_json
|
75
|
+
# => {"posts":[{"title":"Some title"},{"title":"Some title"},{"title":"Some title"}]}
|
76
|
+
|
77
|
+
Post.all.entity(root: 'myposts').to_json
|
78
|
+
# => {"myposts":[{"title":"Some title"},{"title":"Some title"},{"title":"Some title"}]}
|
79
|
+
|
80
|
+
Post.all.entity(root: 'myposts').to_json == Post::Entity.represent(Post.all, root: 'myposts').to_json
|
81
|
+
# => true
|
82
|
+
|
83
|
+
Post.first.entity.to_json
|
84
|
+
# => {"post":[{"title":"Some title"}]}
|
71
85
|
|
72
|
-
Post.first.entity
|
73
|
-
# =>
|
74
|
-
|
86
|
+
Post.first.entity(root: false).to_json
|
87
|
+
# => {"title":"Some title"}
|
88
|
+
```
|
89
|
+
|
90
|
+
In grape controller using json formatter:
|
91
|
+
```ruby
|
92
|
+
get '/posts' do
|
93
|
+
status 200
|
94
|
+
present Post.all.entity
|
95
|
+
end
|
75
96
|
```
|
76
97
|
|
77
98
|
## Contributing
|
data/greenin.gemspec
CHANGED
@@ -9,7 +9,8 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ['Russkikh Artem']
|
10
10
|
spec.email = ['russkikhartem@itbeaver.co']
|
11
11
|
spec.summary = 'Grape Entities in Rails models'
|
12
|
-
spec.description = 'Describe your Grape Entities
|
12
|
+
spec.description = 'Describe your Grape Entities
|
13
|
+
in Rails Active Record models'
|
13
14
|
spec.homepage = 'http://itbeaver.co'
|
14
15
|
spec.license = 'MIT'
|
15
16
|
|
@@ -18,6 +19,17 @@ Gem::Specification.new do |spec|
|
|
18
19
|
spec.test_files = spec.files.grep(/^(test|spec|features)\//)
|
19
20
|
spec.require_paths = ['lib']
|
20
21
|
|
21
|
-
spec.
|
22
|
+
spec.add_runtime_dependency 'activerecord', '>= 0'
|
23
|
+
spec.add_runtime_dependency 'railties', '>= 0'
|
24
|
+
|
25
|
+
spec.add_development_dependency 'grape', '~> 0.11.0'
|
26
|
+
spec.add_development_dependency 'grape-entity', '~> 0.4.4'
|
27
|
+
spec.add_development_dependency 'rspec-rails', '~> 3.2.0'
|
28
|
+
spec.add_development_dependency 'factory_girl_rails', '~> 4.5.0'
|
29
|
+
spec.add_development_dependency 'database_cleaner', '~> 1.4.0'
|
30
|
+
spec.add_development_dependency 'sqlite3', '~> 1.3.10'
|
31
|
+
|
32
|
+
spec.add_development_dependency 'byebug', '>= 0'
|
33
|
+
spec.add_development_dependency 'bundler', '>= 1.6'
|
22
34
|
spec.add_development_dependency 'rake', '~> 10.0'
|
23
35
|
end
|
@@ -3,17 +3,27 @@ module ActiveRecord
|
|
3
3
|
#
|
4
4
|
# Examples
|
5
5
|
#
|
6
|
-
# User.all.entity
|
7
|
-
# # =>
|
8
|
-
#
|
6
|
+
# User.all.entity(root: 'users').to_json
|
7
|
+
# # => { "users":
|
8
|
+
# [{
|
9
|
+
# "title": "Some title"
|
10
|
+
# },
|
11
|
+
# {
|
12
|
+
# "title": "Some title"
|
13
|
+
# },
|
14
|
+
# {
|
15
|
+
# "title": "Some title"
|
16
|
+
# }]
|
17
|
+
# }
|
18
|
+
|
9
19
|
class Relation
|
10
|
-
define_method(Greenin.entity_method_name) do
|
20
|
+
define_method(Greenin.entity_method_name) do |args={}|
|
11
21
|
begin
|
12
22
|
entity = %(#{model}::#{Greenin.entity_class_name}).constantize
|
13
23
|
rescue NameError
|
14
24
|
entity = nil
|
15
25
|
end
|
16
|
-
entity.
|
26
|
+
entity.represent(self, args) if entity
|
17
27
|
end
|
18
28
|
end
|
19
29
|
|
@@ -24,9 +34,8 @@ module ActiveRecord
|
|
24
34
|
# User.entity
|
25
35
|
# # => User::Entity
|
26
36
|
#
|
27
|
-
# User.
|
28
|
-
# # =>
|
29
|
-
# #<User id: nil, ..., created_at: nil, updated_at: nil>, @options={}>
|
37
|
+
# User.first.entity(root: false).to_json
|
38
|
+
# # => {"title":"Some title"}
|
30
39
|
class Base
|
31
40
|
define_singleton_method Greenin.entity_method_name do
|
32
41
|
begin
|
@@ -37,14 +46,14 @@ module ActiveRecord
|
|
37
46
|
entity
|
38
47
|
end
|
39
48
|
|
40
|
-
define_method(Greenin.entity_method_name) do
|
49
|
+
define_method(Greenin.entity_method_name) do |args={}|
|
41
50
|
begin
|
42
51
|
entity = %(#{self.class}::#{Greenin.entity_class_name})
|
43
52
|
.constantize
|
44
53
|
rescue NameError
|
45
54
|
entity = nil
|
46
55
|
end
|
47
|
-
entity.
|
56
|
+
entity.represent(self, args) if entity
|
48
57
|
end
|
49
58
|
end
|
50
59
|
end
|
data/lib/greenin/version.rb
CHANGED
@@ -0,0 +1,28 @@
|
|
1
|
+
== README
|
2
|
+
|
3
|
+
This README would normally document whatever steps are necessary to get the
|
4
|
+
application up and running.
|
5
|
+
|
6
|
+
Things you may want to cover:
|
7
|
+
|
8
|
+
* Ruby version
|
9
|
+
|
10
|
+
* System dependencies
|
11
|
+
|
12
|
+
* Configuration
|
13
|
+
|
14
|
+
* Database creation
|
15
|
+
|
16
|
+
* Database initialization
|
17
|
+
|
18
|
+
* How to run the test suite
|
19
|
+
|
20
|
+
* Services (job queues, cache servers, search engines, etc.)
|
21
|
+
|
22
|
+
* Deployment instructions
|
23
|
+
|
24
|
+
* ...
|
25
|
+
|
26
|
+
|
27
|
+
Please feel free to use a different markup language if you do not plan to run
|
28
|
+
<tt>rake doc:app</tt>.
|
data/spec/dummy/Rakefile
ADDED
File without changes
|
@@ -0,0 +1,13 @@
|
|
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/sstephenson/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= 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
|
+
*/
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Dummy</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>
|
data/spec/dummy/bin/rake
ADDED
@@ -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
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require File.expand_path('../boot', __FILE__)
|
2
|
+
|
3
|
+
require 'active_record/railtie'
|
4
|
+
require 'byebug'
|
5
|
+
require 'grape'
|
6
|
+
require 'grape-entity'
|
7
|
+
require 'greenin'
|
8
|
+
# Pick the frameworks you want:
|
9
|
+
# require 'active_model/railtie'
|
10
|
+
# require 'active_job/railtie'
|
11
|
+
# require 'action_controller/railtie'
|
12
|
+
# require 'action_view/railtie'
|
13
|
+
# require 'sprockets/railtie'
|
14
|
+
# require "rails/test_unit/railtie"
|
15
|
+
|
16
|
+
# Require the gems listed in Gemfile, including any gems
|
17
|
+
# you've limited to :test, :development, or :production.
|
18
|
+
Bundler.require(*Rails.groups)
|
19
|
+
|
20
|
+
module Dummy
|
21
|
+
class Application < Rails::Application
|
22
|
+
# Settings in config/environments/* take precedence over those specified here.
|
23
|
+
# Application configuration should go into files in config/initializers
|
24
|
+
# -- all .rb files in that directory are automatically loaded.
|
25
|
+
|
26
|
+
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
|
27
|
+
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
|
28
|
+
# config.time_zone = 'Central Time (US & Canada)'
|
29
|
+
|
30
|
+
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
|
31
|
+
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
|
32
|
+
# config.i18n.default_locale = :de
|
33
|
+
|
34
|
+
# Do not swallow errors in after_commit/after_rollback callbacks.
|
35
|
+
config.active_record.raise_in_transactional_callbacks = true
|
36
|
+
config.generators do |g|
|
37
|
+
g.test_framework :rspec, :fixture => false
|
38
|
+
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
|
39
|
+
g.assets false
|
40
|
+
g.helper false
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# SQLite version 3.x
|
2
|
+
# gem install sqlite3
|
3
|
+
#
|
4
|
+
# Ensure the SQLite 3 gem is defined in your Gemfile
|
5
|
+
# gem 'sqlite3'
|
6
|
+
#
|
7
|
+
default: &default
|
8
|
+
adapter: sqlite3
|
9
|
+
pool: 5
|
10
|
+
timeout: 5000
|
11
|
+
|
12
|
+
development:
|
13
|
+
<<: *default
|
14
|
+
database: db/development.sqlite3
|
15
|
+
|
16
|
+
# Warning: The database defined as "test" will be erased and
|
17
|
+
# re-generated from your development database when you run "rake".
|
18
|
+
# Do not set this db to the same as development or production.
|
19
|
+
test:
|
20
|
+
<<: *default
|
21
|
+
database: db/test.sqlite3
|
22
|
+
|
23
|
+
production:
|
24
|
+
<<: *default
|
25
|
+
database: db/production.sqlite3
|