acts_as_contextable 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,13 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <projectDescription>
3
+ <name>acts_as_contextable</name>
4
+ <comment></comment>
5
+ <projects>
6
+ </projects>
7
+ <buildSpec>
8
+ </buildSpec>
9
+ <natures>
10
+ <nature>org.radrails.rails.core.railsnature</nature>
11
+ <nature>com.aptana.ruby.core.rubynature</nature>
12
+ </natures>
13
+ </projectDescription>
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in acts_as_contextable.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Sergiy Nikitin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,70 @@
1
+ # Acts As Contextable
2
+
3
+ Acts As Contextable is aruby gem written for Rails/ActiveRecord models.
4
+ The gem allows:
5
+ - Any model can refer to any other model by adding it to its own context
6
+ - Any model can have multiple various models in context
7
+ - Context can be queryed by the class (type) of a model.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'acts_as_contextable'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install acts_as_contextable
22
+
23
+ ### Database Migrations
24
+
25
+ Acts As Contextable uses a votes table to store all voting information.
26
+ To generate and run the migration just use.
27
+
28
+ rails generate acts_as_votable:migration
29
+ rake db:migrate
30
+
31
+ ## Usage
32
+
33
+ ### Contextable Models
34
+
35
+ ```ruby
36
+ class Meeting < ActiveRecord::Base
37
+ acts_as_contextable
38
+ end
39
+
40
+ @meeting = Meeting.new(:name => 'my meeting!')
41
+ @meeting.save
42
+
43
+ @meeting.add_to_context @user
44
+ @meeting.add_to_context @place
45
+
46
+ ```
47
+
48
+ ### Working with context
49
+
50
+ ```ruby
51
+ if @meeting.has_contexts_of_a_type User
52
+ @meeting.contexts_of_a_type(User).each do |user|
53
+ ...
54
+ end
55
+ end
56
+ ```
57
+
58
+ ### Removing from context
59
+
60
+ ```ruby
61
+ @meeting.remove_from_context @user
62
+ ```
63
+
64
+ ## Contributing
65
+
66
+ 1. Fork it
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
69
+ 4. Push to the branch (`git push origin my-new-feature`)
70
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'acts_as_contextable/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "acts_as_contextable"
8
+ spec.version = ActsAsContextable::VERSION
9
+ spec.authors = ["Sergiy Nikitin"]
10
+ spec.email = ["sn@inno-w.com"]
11
+ spec.description = %q{This gem provides basic linking functionality of active record objects. In cases when hard-coded references are not needed, you may refer to other objects by adding them to context}
12
+ spec.summary = %q{Context linking is a flexible mechanizm for dynamic referencing between active record objects.}
13
+ spec.homepage = "https://github.com/senikiti/acts_as_contextable"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,22 @@
1
+ require "acts_as_contextable/version"
2
+ require 'active_record'
3
+ require 'active_support/inflector'
4
+
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+
7
+ module ActsAsContextable
8
+
9
+ if defined?(ActiveRecord::Base)
10
+ require 'acts_as_contextable/extenders/contextable'
11
+ require 'acts_as_contextable/extenders/context'
12
+ require 'acts_as_contextable/context_reference'
13
+ ActiveRecord::Base.extend ActsAsContextable::Extenders::Contextable
14
+ ActiveRecord::Base.extend ActsAsContextable::Extenders::Context
15
+ end
16
+
17
+ end
18
+
19
+ require 'acts_as_contextable/extenders/controller'
20
+ ActiveSupport.on_load(:action_controller) do
21
+ include ActsAsContextable::Extenders::Controller
22
+ end
@@ -0,0 +1,19 @@
1
+ module ActsAsContextable
2
+ module Context
3
+ def self.included(base)
4
+
5
+ base.class_eval do
6
+
7
+ has_many :in_context_of, :class_name => 'ActsAsContextable::ContextReference', :as => :context, :dependent => :destroy do
8
+
9
+ def contextables
10
+ includes(:contextable).map(&:contextable)
11
+ end
12
+
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,10 @@
1
+ module ActsAsContextable
2
+ class ContextReference < ActiveRecord::Base
3
+ belongs_to :contextable, polymorphic: true
4
+ belongs_to :context, polymorphic: true
5
+
6
+ validates_presence_of :contextable_id
7
+ validates_presence_of :context_id
8
+
9
+ end
10
+ end
@@ -0,0 +1,81 @@
1
+ module ActsAsContextable
2
+ module Contextable
3
+ def self.included(base)
4
+ base.class_eval do
5
+ has_many :context_items, :class_name => 'ActsAsContextable::ContextReference', :as => :contextable, :dependent => :destroy do
6
+ def contexts
7
+ includes(:context).map(&:context)
8
+ end
9
+ end
10
+ end
11
+
12
+ end
13
+
14
+ def add_to_context context, options = {}
15
+ self.add_to_ctx :context => context #, :context_weight => options[:context_weight]
16
+ end
17
+
18
+ # put to context
19
+ def add_to_ctx args = {}
20
+
21
+ options = {}.merge(args)
22
+
23
+ if options[:context].nil?
24
+ return false
25
+ end
26
+
27
+ # find the context
28
+ _context_refs_ = find_contexts_for({
29
+ :context_id => options[:context].id,
30
+ :context_type => options[:context].class.base_class.name
31
+ })
32
+
33
+ if _context_refs_.count == 0 or options[:duplicate]
34
+ # this item has never been in context
35
+ context_ref = ActsAsContextable::ContextReference.new(
36
+ :contextable => self,
37
+ :context => options[:context]
38
+ )
39
+ else
40
+ # this context is potentially changing his vote
41
+ context_ref = _context_refs_.last
42
+ end
43
+
44
+ context_ref.save
45
+ end
46
+
47
+ def remove_from_context context, options = {}
48
+ self.remove_from_ctx :context => context
49
+ end
50
+
51
+ def remove_from_ctx args = {}
52
+ return false if args[:context].nil?
53
+ _context_refs_ = find_contexts_for(:context_id => args[:context].id, :context_type => args[:context].class.base_class.name)
54
+
55
+ return true if _context_refs_.size == 0
56
+ _context_refs_.each(&:destroy)
57
+ return true
58
+ end
59
+
60
+ def has_context?
61
+ find_contexts_for().count > 0
62
+ end
63
+
64
+ def contexts
65
+ find_contexts_for().map(&:context)
66
+ end
67
+
68
+ def has_contexts_of_a_type? ctxclass
69
+ find_contexts_for(:context_type => ctxclass.base_class.name).count > 0
70
+ end
71
+
72
+ def contexts_of_a_type ctxclass
73
+ find_contexts_for(:context_type => ctxclass.base_class.name).map(&:context)
74
+ end
75
+
76
+ def find_contexts_for extra_conditions = {}
77
+ context_items.where(extra_conditions)
78
+ end
79
+
80
+ end
81
+ end
@@ -0,0 +1,24 @@
1
+ module ActsAsContextable
2
+ module Extenders
3
+
4
+ module Context
5
+
6
+ def context?
7
+ false
8
+ end
9
+
10
+ def acts_as_context(*args)
11
+ require 'acts_as_contextable/context'
12
+ include ActsAsContextable::Context
13
+
14
+ class_eval do
15
+ def self.context?
16
+ true
17
+ end
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ module ActsAsContextable
2
+ module Extenders
3
+
4
+ module Contextable
5
+
6
+ def contextable?
7
+ false
8
+ end
9
+
10
+ def acts_as_contextable
11
+ require 'acts_as_contextable/contextable'
12
+ include ActsAsContextable::Contextable
13
+
14
+ class_eval do
15
+ def self.contextable?
16
+ true
17
+ end
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,19 @@
1
+ module ActsAsContextable
2
+ module Extenders
3
+
4
+ module Controller
5
+ =begin
6
+ def context_params(params_object = params[:context_reference])
7
+ params_object.permit(:votable_id, :votable_type,
8
+ :voter_id, :voter_type,
9
+ :votable, :voter,
10
+ :vote_flag, :vote_scope)
11
+ end
12
+
13
+ def contextable_params(params_object = params[:context_reference])
14
+ params_object.permit(:vote_registered)
15
+ end
16
+ =end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsContextable
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,31 @@
1
+ require 'rails/generators/migration'
2
+
3
+ module ActsAsContextable
4
+ class MigrationGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+
7
+ desc "Generates migration for contextable (context_references 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/acts_as_contextable_migration.rb'
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,18 @@
1
+ class ActsAsContextableMigration < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :context_references do |t|
4
+ t.references :contextable, :polymorphic => true
5
+ t.references :context, :polymorphic => true
6
+ t.timestamps
7
+ end
8
+
9
+ if ActiveRecord::VERSION::MAJOR < 4
10
+ add_index :context_references, [:contextable_id, :contextable_type]
11
+ add_index :context_references, [:context_id, :context_type]
12
+ end
13
+ end
14
+
15
+ def self.down
16
+ drop_table :context_references
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_contextable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sergiy Nikitin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-10-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: This gem provides basic linking functionality of active record objects.
47
+ In cases when hard-coded references are not needed, you may refer to other objects
48
+ by adding them to context
49
+ email:
50
+ - sn@inno-w.com
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - .gitignore
56
+ - .project
57
+ - Gemfile
58
+ - LICENSE.txt
59
+ - README.md
60
+ - Rakefile
61
+ - acts_as_contextable.gemspec
62
+ - lib/acts_as_contextable.rb
63
+ - lib/acts_as_contextable/context.rb
64
+ - lib/acts_as_contextable/context_reference.rb
65
+ - lib/acts_as_contextable/contextable.rb
66
+ - lib/acts_as_contextable/extenders/context.rb
67
+ - lib/acts_as_contextable/extenders/contextable.rb
68
+ - lib/acts_as_contextable/extenders/controller.rb
69
+ - lib/acts_as_contextable/version.rb
70
+ - lib/generators/acts_as_contextable/migration/migration_generator.rb
71
+ - lib/generators/acts_as_contextable/migration/templates/active_record/migration.rb
72
+ homepage: https://github.com/senikiti/acts_as_contextable
73
+ licenses:
74
+ - MIT
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 1.8.25
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: Context linking is a flexible mechanizm for dynamic referencing between active
97
+ record objects.
98
+ test_files: []
99
+ has_rdoc: