make_flagable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 kgalli
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Manifest ADDED
@@ -0,0 +1,13 @@
1
+ MIT-LICENSE
2
+ README.rdoc
3
+ Rakefile
4
+ generators/make_flagable_migration_generator.rb
5
+ generators/templates/migration.rb
6
+ init.rb
7
+ lib/flag.rb
8
+ lib/flagging.rb
9
+ lib/make_flagable.rb
10
+ tasks/make_flagable_tasks.rake
11
+ test/make_flagable_test.rb
12
+ test/test_helper.rb
13
+ Manifest
data/README.rdoc ADDED
@@ -0,0 +1,45 @@
1
+ = MakeFlagable
2
+
3
+ Rails gem to add flagging functionality to a specific ActiveRecord model.
4
+
5
+ == Installation
6
+
7
+ In environment.rb:
8
+
9
+ Rails::Initializer.run do |config|
10
+ config.gem 'make_flagable', :lib => 'make_flagable', :source => 'http://gems.github.com'
11
+ end
12
+
13
+ At your application root, run:
14
+
15
+ $ sudo rake gems:install
16
+
17
+ Next, generate and run:
18
+
19
+ $ script/generate make_flagable_migration
20
+ $ rake db:migrate
21
+
22
+ == Usage
23
+
24
+ To add flagging functionality to the ActiveRecord model, simply add make_flagable to your class:
25
+
26
+ class Entry < ActiveRecord::Base
27
+ make_flagable
28
+
29
+ validates_presence_of :description
30
+
31
+ def description
32
+ "#{description}"
33
+ end
34
+ end
35
+
36
+ To add a flag you can use the add_flag() mehtod:
37
+
38
+ e = Entry.new
39
+ # assuming you get the user_id by current_user.id
40
+ @e.add_flag(:flag_type => 'spam', :user_id => current_user.id)
41
+
42
+
43
+ == Licence
44
+
45
+ Copyright (c) 2009 kgalli, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('make_flagable', '0.1.0') do |p|
6
+ p.description = "A Rails Plugin to add flagging functionality to any model you like."
7
+ p.url = "http://github.com/kgalli/MakeFlagable"
8
+ p.author = "kgalli"
9
+ p.email = "mail@kgalli.de"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*rake"].sort.each { |ext| load ext }
@@ -0,0 +1,11 @@
1
+ class MakeFlagableMigrationGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.migration_template 'migration.rb', 'db/migrate'
5
+ end
6
+ end
7
+
8
+ def file_name
9
+ 'create_flags_and_flaggings'
10
+ end
11
+ end
@@ -0,0 +1,31 @@
1
+ class CreateFlagsAndFlaggings < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :flags do |t|
4
+ t.string :identifier
5
+ t.string :flag_type
6
+ t.text :description
7
+
8
+ t.timestamps
9
+ end
10
+
11
+ create_table :flaggings do |t|
12
+ t.integer :flag_id
13
+ t.string :flagable_id
14
+ t.string :flagable_type
15
+ t.integer :user_id
16
+ t.text :description
17
+
18
+ t.timestamps
19
+ end
20
+ add_index "flags", ["flag_type"], :name => "fk_flags_flag_type"
21
+ add_index "flaggings", ["user_id"], :name => "fk_flaggings_user"
22
+ add_index "flaggings", ["flagable_id"], :name => "fk_flaggings_flagable_id"
23
+ add_index "flaggings", ["flagable_type"], :name => "fk_flaggings_flagable_type"
24
+
25
+ end
26
+
27
+ def self.down
28
+ drop_table :flags
29
+ drop_table :flaggings
30
+ end
31
+ end
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # Include hook code here
2
+ require 'make_flagable'
data/lib/flag.rb ADDED
@@ -0,0 +1,31 @@
1
+ class Flag < ActiveRecord::Base
2
+ include Comparable
3
+
4
+ # relationships
5
+ has_many :flaggings
6
+ has_many :make_flagables, :through => :flaggings
7
+
8
+ # virtual attributes used to store additional
9
+ # information about the flaggings for a model
10
+ attr_accessor :user_id, :flag_description
11
+
12
+ # avoid to flag a model twice with specific
13
+ # flag type
14
+ validates_uniqueness_of :flag_type
15
+
16
+ # Helper class method to lookup all flags assigned
17
+ # to all make_flabable types for a given user.
18
+ def self.find_flags_by_user(user)
19
+ find(:all,
20
+ :conditions => ["user_id = ?", user.id],
21
+ :order => "created_at DESC"
22
+ )
23
+ end
24
+
25
+ # implemented to be able to use the conventional
26
+ # comparison operators accessible via include Comparable
27
+ # -->: <, <=, ==, >=, >
28
+ def <=>(other)
29
+ flag_type.to_s <=> other.flag_type.to_s
30
+ end
31
+ end
data/lib/flagging.rb ADDED
@@ -0,0 +1,25 @@
1
+ class Flagging < ActiveRecord::Base
2
+ belongs_to :flag
3
+ belongs_to :flagable, :polymorphic => true
4
+ belongs_to :user
5
+
6
+ validates_uniqueness_of :user_id, :scope => [:flag_id, :flagable_id, :flagable_type]
7
+
8
+ def self.has_flagged?(flagable)
9
+ begin
10
+ Flag.find_flags_for_make_flagable(flagable.type, flagable.id)
11
+ return true
12
+ rescue
13
+ return false
14
+ end
15
+ end
16
+
17
+ def self.find_or_create_by_user_id_flable_id_flable_type(arg)
18
+ find(:all, :select => '*', :conditions => ["user_id = ? AND flagable_id = ? AND flagable_type = ? ",
19
+ arg[:user_id], arg[:flagable_id],arg[:flagable_type]],
20
+ :limit => 1 ) || create(arg)
21
+ end
22
+ #def flagable_type=(type)
23
+ # super(type.to_s.classify.constantize.base_class.to_s)
24
+ #end
25
+ end
@@ -0,0 +1,104 @@
1
+ module MakeFlagable
2
+
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def make_flagable
9
+ has_many :flaggings, :as => :flagable
10
+ has_many :flags, :through => :flaggings
11
+ include MakeFlagable::InstanceMethods # --> instance methods xx_xx()
12
+ extend MakeFlagable::SingletonMethods # --> class methods self.xx_xx()
13
+ end
14
+ end
15
+
16
+ module SingletonMethods
17
+ # returns all flagged objects
18
+ # (each object only ones --> group)
19
+ def flagged
20
+ find(:all, :joins => {:flags => :flaggings}, :group => "#{make_flagable}.id")
21
+ end
22
+
23
+ # returns all objects flagged with a given flag_type
24
+ def flagged_with_flag_type(flag_type)
25
+ find(:all, :joins => {:flags => :flaggings}, :conditions => ["flags.flag_type = ?", flag_type],
26
+ :group => "#{pluralized_class_name(self)}.id, flags.flag_type")
27
+ end
28
+
29
+ private
30
+ # return the class name of a given object
31
+ def class_name(obj)
32
+ ActiveRecord::Base.send(:class_name_of_active_record_descendant, obj).to_s
33
+ end
34
+
35
+ # returns pluralized class name of a given object
36
+ def pluralized_class_name(obj)
37
+ class_name(obj).to_s.pluralize.downcase
38
+ end
39
+
40
+ end
41
+
42
+ module InstanceMethods
43
+
44
+ # returns if the flagable model is flagged
45
+ def flagged?
46
+ flags.empty?
47
+ end
48
+
49
+ # returns if the flagable model is flagged with
50
+ # a given flag type
51
+ def flagged_as_flag_type?(flag_type)
52
+ if flags.find(:first, :conditions => ["flag_type = ?", flag_type]) != nil
53
+ return true
54
+ else
55
+ return false
56
+ end
57
+ end
58
+
59
+ # returns if the flagable model is flagged by a given user
60
+ # (flag type doesn't matter)
61
+ def flagged_by_user?(user)
62
+ if flags.find(:first, :conditions => ["flaggings.user_id = ?", user.id]) != nil
63
+ true
64
+ else
65
+ false
66
+ end
67
+ end
68
+
69
+ # returns the flags for the flagable model
70
+ # (grouped by flag type --> no duplicates)
71
+ def flags_grouped_by_flag_type
72
+ flags.find(:all, :group => "flags.flag_type", :order => 'count(flags.flag_type) DESC' )
73
+ end
74
+
75
+
76
+ # delete all flags with specific flag_type
77
+ # from flagable model
78
+ def unflag_by_flag_type(flag_type)
79
+ flaggings_to_destroy = self.flaggings.find(:all, :joins => :flag, :conditions => ["flags.flag_type = ?", flag_type])
80
+ self.flaggings.destroy(flaggings_to_destroy) if flaggings_to_destroy != nil
81
+ end
82
+
83
+ # helper method to flag a flagable model
84
+ # by setting up the right flagging associations
85
+ # user_id and description are virtual attributes
86
+ # provided by the flag model
87
+ def add_flag(options)
88
+ flag = Flag.find_or_create_by_flag_type(options[:flag_type].to_s)
89
+ flag.user_id = options[:user_id]
90
+ flag.flag_description = options[:flag_description]
91
+
92
+ flagging = Flagging.find(:first, :conditions => ["flaggings.flagable_id = ? AND flaggings.user_id = ? AND flaggings.flagable_type = ? AND flag_id = ?",
93
+ self.id, flag.user_id, self.class.name.to_s, flag.id], :limit => 1)
94
+ flagging = Flagging.new if flagging == nil
95
+ flagging.description = flag.flag_description
96
+ flagging.flagable = self
97
+ flagging.flag = flag
98
+ flagging.user_id = flag.user_id
99
+ flagging.save
100
+ end
101
+ end
102
+ end
103
+
104
+ ActiveRecord::Base.send(:include, MakeFlagable)
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{make_flagable}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["kgalli"]
9
+ s.date = %q{2009-10-31}
10
+ s.description = %q{A Rails Plugin to add flagging functionality to any model you like.}
11
+ s.email = %q{mail@kgalli.de}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/flag.rb", "lib/flagging.rb", "lib/make_flagable.rb", "tasks/make_flagable_tasks.rake"]
13
+ s.files = ["MIT-LICENSE", "README.rdoc", "Rakefile", "generators/make_flagable_migration_generator.rb", "generators/templates/migration.rb", "init.rb", "lib/flag.rb", "lib/flagging.rb", "lib/make_flagable.rb", "tasks/make_flagable_tasks.rake", "test/make_flagable_test.rb", "test/test_helper.rb", "Manifest", "make_flagable.gemspec"]
14
+ s.homepage = %q{http://github.com/kgalli/MakeFlagable}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Make_flagable", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{make_flagable}
18
+ s.rubygems_version = %q{1.3.3}
19
+ s.summary = %q{A Rails Plugin to add flagging functionality to any model you like.}
20
+ s.test_files = ["test/make_flagable_test.rb", "test/test_helper.rb"]
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 3
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ else
28
+ end
29
+ else
30
+ end
31
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :make_flagable do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class MakeFlagableTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: make_flagable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - kgalli
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-31 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A Rails Plugin to add flagging functionality to any model you like.
17
+ email: mail@kgalli.de
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ - lib/flag.rb
25
+ - lib/flagging.rb
26
+ - lib/make_flagable.rb
27
+ - tasks/make_flagable_tasks.rake
28
+ files:
29
+ - MIT-LICENSE
30
+ - README.rdoc
31
+ - Rakefile
32
+ - generators/make_flagable_migration_generator.rb
33
+ - generators/templates/migration.rb
34
+ - init.rb
35
+ - lib/flag.rb
36
+ - lib/flagging.rb
37
+ - lib/make_flagable.rb
38
+ - tasks/make_flagable_tasks.rake
39
+ - test/make_flagable_test.rb
40
+ - test/test_helper.rb
41
+ - Manifest
42
+ - make_flagable.gemspec
43
+ has_rdoc: true
44
+ homepage: http://github.com/kgalli/MakeFlagable
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options:
49
+ - --line-numbers
50
+ - --inline-source
51
+ - --title
52
+ - Make_flagable
53
+ - --main
54
+ - README.rdoc
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "1.2"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project: make_flagable
72
+ rubygems_version: 1.3.5
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: A Rails Plugin to add flagging functionality to any model you like.
76
+ test_files:
77
+ - test/make_flagable_test.rb
78
+ - test/test_helper.rb