greenin 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +11 -0
  5. data/Gemfile +4 -0
  6. data/README.md +32 -11
  7. data/greenin.gemspec +14 -2
  8. data/lib/active_record_overrides.rb +19 -10
  9. data/lib/greenin/version.rb +1 -1
  10. data/spec/dummy/README.rdoc +28 -0
  11. data/spec/dummy/Rakefile +6 -0
  12. data/spec/dummy/app/api/base_api.rb +10 -0
  13. data/spec/dummy/app/api/dummy_api.rb +2 -0
  14. data/spec/dummy/app/assets/images/.keep +0 -0
  15. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  16. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  17. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  18. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  19. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  20. data/spec/dummy/app/mailers/.keep +0 -0
  21. data/spec/dummy/app/models/.keep +0 -0
  22. data/spec/dummy/app/models/concerns/.keep +0 -0
  23. data/spec/dummy/app/models/post.rb +6 -0
  24. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  25. data/spec/dummy/bin/bundle +3 -0
  26. data/spec/dummy/bin/rails +4 -0
  27. data/spec/dummy/bin/rake +4 -0
  28. data/spec/dummy/bin/setup +29 -0
  29. data/spec/dummy/config.ru +4 -0
  30. data/spec/dummy/config/application.rb +43 -0
  31. data/spec/dummy/config/boot.rb +5 -0
  32. data/spec/dummy/config/database.yml +25 -0
  33. data/spec/dummy/config/environment.rb +5 -0
  34. data/spec/dummy/config/environments/development.rb +38 -0
  35. data/spec/dummy/config/environments/production.rb +79 -0
  36. data/spec/dummy/config/environments/test.rb +37 -0
  37. data/spec/dummy/config/initializers/assets.rb +11 -0
  38. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  39. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  40. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  41. data/spec/dummy/config/initializers/inflections.rb +16 -0
  42. data/spec/dummy/config/initializers/initializer.rb +4 -0
  43. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  44. data/spec/dummy/config/initializers/session_store.rb +3 -0
  45. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  46. data/spec/dummy/config/locales/en.yml +23 -0
  47. data/spec/dummy/config/routes.rb +57 -0
  48. data/spec/dummy/config/secrets.yml +22 -0
  49. data/spec/dummy/db/development.sqlite3 +0 -0
  50. data/spec/dummy/db/migrate/20150224174059_create_posts.rb +9 -0
  51. data/spec/dummy/db/schema.rb +22 -0
  52. data/spec/dummy/db/test.sqlite3 +0 -0
  53. data/spec/dummy/lib/assets/.keep +0 -0
  54. data/spec/dummy/log/.keep +0 -0
  55. data/spec/dummy/public/404.html +67 -0
  56. data/spec/dummy/public/422.html +67 -0
  57. data/spec/dummy/public/500.html +66 -0
  58. data/spec/dummy/public/favicon.ico +0 -0
  59. data/spec/factories/posts.rb +5 -0
  60. data/spec/models/active_record_spec.rb +18 -0
  61. data/spec/models/api_spec.rb +50 -0
  62. data/spec/models/settings_spec.rb +6 -0
  63. data/spec/rails_helper.rb +65 -0
  64. data/spec/spec_helper.rb +95 -0
  65. metadata +247 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 741c1937512012abe2964fddcf304fd44d0ba585
4
- data.tar.gz: a71adc705bcb5b867b79f0d85599b3576d7eaec6
3
+ metadata.gz: 2033e9cdfa4ed544139dc352a45fdda77afba6af
4
+ data.tar.gz: 4dcd42334e6957f04ecec0e8567dc2a5e9c93484
5
5
  SHA512:
6
- metadata.gz: 4d553d4d337d0c21b772de0162060bcdc0411a416f42ded668318719edb7da7a4b79ea1242c0f17fa58a37f091578d76773c6a818962be5c51f8a565b649896a
7
- data.tar.gz: 0536be393b5ff9b634b24c2f92481b9ff97f636d4516630872584acae40b62a9533f54b1a1d4f8f9244a5eec566d1a518d341b0f1cfcfb8eeee97aca6b82cc19
6
+ metadata.gz: 05afeec084aff3a535804d795c9c0a300c08a7a9c183b3a3a9738dd0247774835d06da58f396ec2458deef939112c46c3d40f15e5d46216d6a4a5f6fd9f27f86
7
+ data.tar.gz: ff27306b5c8a98516660cc72030f53d53a21c132ab13ba3c2a03774bfd7202200d36e6911e9d813f4899fc313aee02c0f4c0456c9eea49c6d00991364ec2dc2a
data/.gitignore CHANGED
@@ -12,3 +12,4 @@
12
12
  *.o
13
13
  *.a
14
14
  mkmf.log
15
+ /spec/dummy/log/test.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require rails_helper
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0
4
+ - 2.1
5
+ - 2.2
6
+ - ruby-head
7
+ bundler_args: --without production
8
+ addons:
9
+ code_climate:
10
+ repo_token: 6a19929ed0a99137c10684ba06ca240aa176173a4ff878248db3ec5f7aecf726
11
+ script: bundle exec rspec spec
data/Gemfile CHANGED
@@ -1,4 +1,8 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in greenin.gemspec
4
+
5
+ gem "codeclimate-test-reporter", group: :test, require: nil
6
+
4
7
  gemspec
8
+
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
- So, now you can call entity like that:
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
- # => Post::Entity
64
-
65
- Post.entity
66
- # => Post::Entity
66
+ Post::Entity == Post.entity
67
+ # => true
67
68
 
68
69
  Post.all.entity
69
- # => #<Post::Entity:0x007fb4610d8a00
70
- # @object=#<ActiveRecord::Relation [...]>, @options={}>
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
- # => #<Post::Entity:0x007fb462263090 @object=
74
- # #<Post id: nil, ..., created_at: nil, updated_at: nil>, @options={}>
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
@@ -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 in Rails Active Record models'
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.add_development_dependency 'bundler', '~> 1.7'
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
- # # => #<User::Entity:0x007fb4610d8a00
8
- # @object=#<ActiveRecord::Relation [...]>, @options={}>
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.new(self) if 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.new.entity
28
- # # => #<User::Entity:0x007fb462263090 @object=
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.new(self) if entity
56
+ entity.represent(self, args) if entity
48
57
  end
49
58
  end
50
59
  end
@@ -1,3 +1,3 @@
1
1
  module Greenin
2
- VERSION = '1.0.1'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -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>.
@@ -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
@@ -0,0 +1,10 @@
1
+ class BaseApi < Grape::API
2
+ def self.inherited(subclass)
3
+ super
4
+ subclass.instance_eval do
5
+ version 'v1', using: :header, vendor: 'dummy', cascade: false
6
+ format :json
7
+ default_format :json
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,2 @@
1
+ class DummyApi < BaseApi
2
+ end
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
+ */
@@ -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,2 @@
1
+ module ApplicationHelper
2
+ end
File without changes
File without changes
File without changes
@@ -0,0 +1,6 @@
1
+ class Post < ActiveRecord::Base
2
+ class Entity < Grape::Entity
3
+ root 'posts', 'post'
4
+ expose :title, documentation: { type: 'String', desc: 'Post title' }
5
+ end
6
+ end
@@ -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>
@@ -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,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ 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
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails.application
@@ -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,5 @@
1
+ # Set up gems listed in the Gemfile.
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
5
+ $LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__)
@@ -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