simple_group 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a58d32b879007dadd0d2faf8571a8fb5579d7d33
4
+ data.tar.gz: fb450d90ad37dc7f926e3f485c9d2d236ef86cf2
5
+ SHA512:
6
+ metadata.gz: e065f746f3a84e17dd4694b67b63eeab9031b371d5105f8a32b2f30af41133a0957196e27c774953a6e23cd143b0489f9a1046112f6d262d9e3e30cf889a1cfc
7
+ data.tar.gz: 2b8df34c46ffc63db95fb469ad6baad21592366d24d9624fa4a53705d62f9fa1a7f6daec4cc03c0ba1dc0b2a12b2dc11c2ac8baab035f729ef2a64edc8ae19b3
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /spec/internal/logs/
10
+ /tmp/
11
+ *.sqlite3
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
4
+ - 2.2.0
5
+ - 2.2.1
6
+ env:
7
+ - RAILS_VERSION=4.2.1
8
+ - RAILS_VERSION=4.2.0
9
+ - RAILS_VERSION=4.1
10
+ - RAILS_VERSION=4.0
11
+ script: bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_group.gemspec
4
+ gemspec
5
+
6
+ rails_version = ENV['RAILS_VERSION'] || 'default'
7
+
8
+ rails = case rails_version
9
+ when 'master'
10
+ { :github => 'rails/rails' }
11
+ when 'default'
12
+ '~> 4.0.0'
13
+ else
14
+ "~> #{rails_version}"
15
+ end
16
+ gem 'rails', rails
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 patorash
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,102 @@
1
+ # simple_group
2
+
3
+ simple_group gem is able to add group function to ActiveRecord.
4
+
5
+ [![Build Status](https://travis-ci.org/patorash/simple_group.svg?branch=master)](https://travis-ci.org/patorash/simple_group)
6
+
7
+ ## Installation
8
+
9
+ ### Rails 4.0+
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'simple_group'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ ### Database Migrations
22
+
23
+ simple_group uses a simple_group_combinations table to store all group information.
24
+ To genarate and run the migration just use.
25
+
26
+ $ bin/rails g simple_group:migration
27
+ $ bin/rake db:migrate
28
+
29
+ ## Usage
30
+
31
+ ### Group item model
32
+
33
+ Animal model
34
+
35
+ ```ruby
36
+ class Animal < ActiveRecord::Base
37
+ groupable
38
+ validates :name, presence: true
39
+ end
40
+ ```
41
+
42
+ Fruit model
43
+
44
+ ```ruby
45
+ class Fruit < ActiveRecord::Base
46
+ groupable
47
+ validates :name, presence: true
48
+ end
49
+ ```
50
+
51
+
52
+ ### Group model
53
+
54
+ ```ruby
55
+ class MyGroup < ActiveRecord::Base
56
+ add_group_function
57
+
58
+ validates :name, presence: true
59
+ validates :user, presence: true
60
+ end
61
+ ```
62
+
63
+ ### Example
64
+
65
+ ```ruby
66
+ user = User.create(name: 'Taro')
67
+ group = MyGroup.create(name: 'Animals', user: user)
68
+
69
+ # group add a dog
70
+ dog = Animal.create(name: 'dog')
71
+ group.add(dog)
72
+
73
+ # cat add to group
74
+ cat = Animal.create(name: 'cat')
75
+ cat.add_to_group(group)
76
+
77
+ # group remove a dog
78
+ group.remove(dog)
79
+
80
+ # item list in group
81
+ group.group_items
82
+
83
+ # Only animal
84
+ group.for_type(Animal)
85
+
86
+ # Only fruit
87
+ group.for_type(Fruit)
88
+ ```
89
+
90
+ ## Development
91
+
92
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
93
+
94
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
95
+
96
+ ## Contributing
97
+
98
+ 1. Fork it ( https://github.com/patorash/simple_group/fork )
99
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
100
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
101
+ 4. Push to the branch (`git push origin my-new-feature`)
102
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "simple_group"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require "pry"
11
+ Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ require 'rails/generators/migration'
2
+
3
+ module SimpleGroup
4
+ class MigrationGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+
7
+ desc "Generators migration for simple_group(simple_groups table)"
8
+
9
+ def self.orm
10
+ Rails::Generators.options[:rails][:orm]
11
+ end
12
+
13
+ def self.source_root
14
+ File.join(File.dirname(__FILE__), 'templates', (orm.to_s unless orm.class.eql?(String)))
15
+ end
16
+
17
+ def self.orm_has_migration?
18
+ [:active_record].include? orm
19
+ end
20
+
21
+ def self.next_migration_number(path)
22
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
23
+ end
24
+
25
+ def create_migration_file
26
+ if self.class.orm_has_migration?
27
+ migration_template 'migration.rb', 'db/migrate/simple_group_migration'
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,18 @@
1
+ class SimpleGroupMigration < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :simple_group_combinations do |t|
4
+ t.references :group_item, polymorphic: true
5
+ t.references :group, polymorphic: true
6
+ t.timestamp
7
+ end
8
+
9
+ if ActiveRecord::VERSION::MAJOR < 4
10
+ add_index :simple_groups, [:group_item_id, :group_item_type]
11
+ add_index :simple_groups, [:group_id, :group_type]
12
+ end
13
+ end
14
+
15
+ def self.down
16
+ drop_table :simple_group_combinations
17
+ end
18
+ end
@@ -0,0 +1,22 @@
1
+ module SimpleGroup
2
+ class Combination < ::ActiveRecord::Base
3
+ self.table_name = 'simple_group_combinations'
4
+
5
+ if ::ActiveRecord::VERSION::MAJOR < 4
6
+ attr_accessible :group_id, :group_type,
7
+ :group_item_id, :group_item_type,
8
+ :group, :group_item
9
+ end
10
+
11
+ belongs_to :group, polymorphic: true
12
+ belongs_to :group_item, polymorphic: true
13
+
14
+ scope :for_type, lambda { |klass| where(group_item_type: klass) }
15
+ scope :by_type, lambda { |klass| where(group_type: klass) }
16
+
17
+ validates :group_item_id, presence: true
18
+ validates :group_id, presence: true
19
+ end
20
+
21
+ class GroupNotAllowError < Exception; end
22
+ end
@@ -0,0 +1,20 @@
1
+ module SimpleGroup
2
+ module Extenders
3
+ module Group
4
+ def added_group_function?
5
+ false
6
+ end
7
+
8
+ def add_group_function(*args)
9
+ require 'simple_group/group'
10
+ include SimpleGroup::Group
11
+
12
+ class_eval do
13
+ def self.added_group_function?
14
+ true
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ module SimpleGroup
2
+ module Extenders
3
+ module GroupItem
4
+ def groupable?
5
+ false
6
+ end
7
+
8
+ def groupable
9
+ require 'simple_group/group_item'
10
+ include SimpleGroup::GroupItem
11
+
12
+ class_eval do
13
+ def self.groupable?
14
+ true
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,41 @@
1
+ module SimpleGroup
2
+ module Group
3
+ def self.included(base)
4
+ base.class_eval do
5
+ has_many :combinations, class_name: 'SimpleGroup::Combination', as: :group, dependent: :destroy do
6
+ def group_items
7
+ includes(:group_item).map(&:group_item)
8
+ end
9
+ end
10
+ end
11
+ end
12
+
13
+ def add(model)
14
+ raise SimpleGroup::GroupNotAllowError unless model.class.groupable?
15
+ SimpleGroup::Combination.create(group: self, group_item: model)
16
+ end
17
+
18
+ def remove(model)
19
+ raise_error unless model.class.groupable?
20
+ group = SimpleGroup::Combination.where(
21
+ group_type: self.class,
22
+ group_id: self,
23
+ group_item_type: model.class,
24
+ group_item_id: model
25
+ ).first
26
+ if group.present?
27
+ group.destroy
28
+ else
29
+ false
30
+ end
31
+ end
32
+
33
+ def for_type(klass)
34
+ self.combinations.for_type(klass)
35
+ end
36
+
37
+ def count
38
+ self.combinations.count
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,18 @@
1
+ module SimpleGroup
2
+ module GroupItem
3
+
4
+ def self.included(base)
5
+ base.class_eval do
6
+ has_many :combinations, :class_name => 'SimpleGroup::Combination', :as => :group_item, :dependent => :destroy do
7
+ def groups
8
+ includes(:group).map(&:group)
9
+ end
10
+ end
11
+ end
12
+ end
13
+
14
+ def add_to_group(group)
15
+ SimpleGroup::Combination.create(group_item: self, group: group)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleGroup
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,16 @@
1
+ require 'active_record'
2
+ require 'active_support/inflector'
3
+
4
+ require "simple_group/version"
5
+
6
+ module SimpleGroup
7
+ if defined?(ActiveRecord::Base)
8
+ require 'simple_group/extenders/groop'
9
+ require 'simple_group/extenders/group_item'
10
+ require "simple_group/group"
11
+ require "simple_group/group_item"
12
+ require 'simple_group/combination'
13
+ ActiveRecord::Base.extend SimpleGroup::Extenders::Group
14
+ ActiveRecord::Base.extend SimpleGroup::Extenders::GroupItem
15
+ end
16
+ end
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'simple_group/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "simple_group"
7
+ spec.version = SimpleGroup::VERSION
8
+ spec.authors = ["Toyoaki Oko"]
9
+ spec.email = ["chariderpato@gmail.com"]
10
+
11
+ spec.summary = %q{simple_group is able to add group function to ActiveRecord.}
12
+ spec.description = %q{simple_group is able to add group function to ActiveRecord.}
13
+ spec.homepage = "https://github.com/patorash/simple_group"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", ">= 1.7.6"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "pry"
23
+ spec.add_development_dependency "rspec", ">= 3.0.0"
24
+ spec.add_development_dependency "sqlite3"
25
+ spec.add_development_dependency "database_rewinder", "~> 0.5.1"
26
+ spec.add_development_dependency 'combustion', '~> 0.5.3'
27
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_group
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Toyoaki Oko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.7.6
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.7.6
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 3.0.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 3.0.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: database_rewinder
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.5.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.5.1
97
+ - !ruby/object:Gem::Dependency
98
+ name: combustion
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.5.3
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.5.3
111
+ description: simple_group is able to add group function to ActiveRecord.
112
+ email:
113
+ - chariderpato@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - bin/console
126
+ - bin/setup
127
+ - lib/generators/simple_group/migration/migration_generator.rb
128
+ - lib/generators/simple_group/migration/templates/active_record/migration.rb
129
+ - lib/simple_group.rb
130
+ - lib/simple_group/combination.rb
131
+ - lib/simple_group/extenders/groop.rb
132
+ - lib/simple_group/extenders/group_item.rb
133
+ - lib/simple_group/group.rb
134
+ - lib/simple_group/group_item.rb
135
+ - lib/simple_group/version.rb
136
+ - simple_group.gemspec
137
+ homepage: https://github.com/patorash/simple_group
138
+ licenses:
139
+ - MIT
140
+ metadata: {}
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 2.4.6
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: simple_group is able to add group function to ActiveRecord.
161
+ test_files: []