is_graffitiable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
File without changes
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 1
3
+ :patch: 0
4
+ :major: 0
@@ -0,0 +1,7 @@
1
+ class IsGraffitiableMigrationGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.migration_template 'migration.rb', 'db/migrate', :migration_file_name => 'is_graffitiable_migration'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,17 @@
1
+ class IsGraffitiableMigration < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :graffitis do |t|
4
+ t.string :name, :default => ''
5
+ t.string :value, :default => ''
6
+ t.string :graffitiable_type, :default => ''
7
+ t.integer :graffitiable_id
8
+ t.timestamps
9
+ end
10
+
11
+ add_index :graffitis, [:name, :graffitiable_id, :graffitiable_type]
12
+ end
13
+
14
+ def self.down
15
+ drop_table :graffitis
16
+ end
17
+ end
data/lib/graffiti.rb ADDED
@@ -0,0 +1,9 @@
1
+ class Graffiti < ActiveRecord::Base
2
+ belongs_to :graffitiable, :polymorphic => true
3
+
4
+ validates_presence_of :name
5
+ validates_presence_of :value
6
+ def to_s
7
+ name
8
+ end
9
+ end
@@ -0,0 +1,60 @@
1
+ module IsGraffitiable
2
+ class GraffitiMap < Hash
3
+ def initialize(args={})
4
+ args.each do |key, value|
5
+ self[key] = value
6
+ end
7
+ end
8
+ end
9
+
10
+ def self.included(base)
11
+ base.extend(ClassMethods)
12
+ end
13
+
14
+ module ClassMethods
15
+ def is_graffitiable
16
+ has_many :graffitis, :as => :graffitiable
17
+
18
+ after_create :save_graffitis
19
+ after_update :save_graffitis
20
+
21
+ include IsGraffitiable::InstanceMethods
22
+ end
23
+ end
24
+
25
+ module InstanceMethods
26
+ def graffiti_map
27
+ @graffiti_map = GraffitiMap.new
28
+ graffitis.each do |graffiti|
29
+ @graffiti_map[graffiti.name] = graffiti.value
30
+ end
31
+ @graffiti_map
32
+ end
33
+
34
+ def save_graffitis
35
+ return unless @graffiti_map
36
+
37
+ new_graffiti_map = @graffiti_map.clone
38
+ updated_graffiti_map = {}
39
+
40
+ graffitis.each do |graffiti|
41
+ new_graffiti_map.delete(graffiti.name)
42
+ end
43
+
44
+ old_graffitis = graffitis.reject{|graffiti| @graffiti_map.keys.include?(graffiti.name)}
45
+
46
+ self.class.transaction do
47
+ if old_graffitis.any?
48
+ old_graffitis.each(&:destroy)
49
+ end
50
+ new_graffiti_map.each do |key,value|
51
+ graffitis << Graffiti.create(:name => key, :value => value)
52
+ end
53
+ end
54
+
55
+ true
56
+ end
57
+ end # InstanceMethods
58
+ end # IsGraffitiable
59
+
60
+ ActiveRecord::Base.send(:include, IsGraffitiable)
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: is_graffitiable
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Darren Dao
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-24 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description:
23
+ email: ddao@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - README.rdoc
32
+ - VERSION.yml
33
+ - generators/is_graffitiable_migration/is_graffitiable_migration_generator.rb
34
+ - generators/is_graffitiable_migration/templates/migration.rb
35
+ - lib/is_graffitiable.rb
36
+ - lib/graffiti.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/giraffesoft/is_graffitiable
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --inline-source
44
+ - --charset=UTF-8
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ hash: 3
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ requirements: []
66
+
67
+ rubyforge_project:
68
+ rubygems_version: 1.3.7
69
+ signing_key:
70
+ specification_version: 2
71
+ summary: tagging that allow you to use key:value tags
72
+ test_files: []
73
+