Permalink
Browse files

added helpers

  • Loading branch information...
1 parent cec85e7 commit 5eb4760377808d563ca66fb1dd8dda73924267a7 @alec-c4 alec-c4 committed Jan 23, 2012
View
35 README.md
@@ -55,44 +55,23 @@ Configure flag_observer.rb to report on Flag creation.
Example Usage:
-Create Flag
+You can set flag on flaggeable (for example - Project) by
- ruby-1.9.3-p0 :004 > f = Project.first.flags.build
- Project Load (0.4ms) SELECT "projects".* FROM "projects" LIMIT 1
- => #<Flag id: nil, sneak_type: nil, sneak_id: nil, flaggeable_type: "Project", flaggeable_id: 1, report_type: nil, created_at: nil, updated_at: nil>
+ Project.first.reported :by => User.first, :as => "spam"
-Set sneak
+or
- ruby-1.9.3-p0 :005 > f.sneak = User.first
- User Load (1.0ms) SELECT "users".* FROM "users" LIMIT 1
- => #<User id: 1, username: "alec", email: "alec@alec-c4.com", encrypted_password: "$2a$10$LsTo5Mbs8ZXKjEyvXMs9F.N6yJRTNYJ76QOCqF60FrWL...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: "2012-01-16 21:53:57", sign_in_count: 9, current_sign_in_at: "2012-01-16 21:53:57", last_sign_in_at: "2012-01-16 21:53:35", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", password_salt: nil, confirmation_token: nil, confirmed_at: "2011-12-22 11:34:33", confirmation_sent_at: "2011-12-22 11:34:30", failed_attempts: 0, unlock_token: nil, locked_at: nil, authentication_token: nil, avatar: nil, roles_mask: 1, notes: nil, created_at: "2011-12-22 11:34:30", updated_at: "2012-01-16 21:53:57">
+ User.first.report :on => Project.first, :with => "spam"
-Set report type or use defaults ("spam", "offensive", "attention")
+As you can see - there are methods - **reported** (for _flaggeable_ model) and **report** (for _sneak_ model).
- ruby-1.9.3-p0 :006 > f.report_type = "spam1"
- => "spam1"
+Method **reported** can be invoked with keys :by (who reporting) and :as (type of report).
-Then save
-
- ruby-1.9.3-p0 :007 > f.save
- (0.1ms) BEGIN
- Flag Load (0.9ms) SELECT "flags".* FROM "flags" WHERE (flaggeable_type = 'Project' and flaggeable_id = 1 and sneak_type = 'User' and sneak_id = 1) LIMIT 1
- SQL (3.2ms) INSERT INTO "flags" ("created_at", "flaggeable_id", "flaggeable_type", "report_type", "sneak_id", "sneak_type", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["created_at", Sun, 22 Jan 2012 10:34:40 UTC +00:00], ["flaggeable_id", 1], ["flaggeable_type", "Project"], ["report_type", "spam1"], ["sneak_id", 1], ["sneak_type", "User"], ["updated_at", Sun, 22 Jan 2012 10:34:40 UTC +00:00]]
- (0.4ms) COMMIT
- => true
-
-And verify
-
- ruby-1.9.3-p0 :008 > Flag.all
- Flag Load (0.4ms) SELECT "flags".* FROM "flags"
- => [#<Flag id: 1, sneak_type: "User", sneak_id: 1, flaggeable_type: "Project", flaggeable_id: 1, report_type: "spam1", created_at: "2012-01-22 10:34:40", updated_at: "2012-01-22 10:34:40">]
+Method **report** can be invoked with keys :on (target for report) and :as (type of report).
## ToDo
-* add helping functions
* add spec tests
-* add generator for views
-
## Contributing
View
2 app/models/flag.rb
@@ -1,6 +1,6 @@
class Flag < ActiveRecord::Base
- attr_accessible :sneak_type, :sneak_id, :flaggeable_id, :flaggeable_type, :report_type
+ attr_accessible :sneak, :flaggeable, :report_type
########## Associations
belongs_to :sneak, :polymorphic => true
View
2 config/locales/en.yml
@@ -1,4 +1,4 @@
en:
flags:
messages:
- already_flagged: Already flagged!
+ already_flagged: ^Already flagged!
View
2 config/locales/ru.yml
@@ -1,4 +1,4 @@
ru:
flags:
messages:
- already_flagged: Флаг уже был установлен!
+ already_flagged: ^Флаг уже был установлен!
View
3 config/routes.rb
@@ -1,3 +0,0 @@
-Rails.application.routes.draw do
- resources :flags
-end
View
7 lib/sneak/active_record_extension.rb
@@ -1,15 +1,22 @@
+require "sneak/flaggeable_methods"
+require "sneak/sneak_methods"
+
module Sneak
module ActiveRecordExtension
def is_flaggeable
has_many :flags, :as => :flaggeable, :dependent => :destroy
accepts_nested_attributes_for :flags
+
+ include FlaggeableMethods
end
def acts_as_sneak
has_many :flags, :as => :sneak
accepts_nested_attributes_for :flags
+
+ include SneakMethods
end
end
end
View
7 lib/sneak/flaggeable_methods.rb
@@ -0,0 +1,7 @@
+module FlaggeableMethods
+ def reported(options = {})
+ options.assert_valid_keys :by, :as
+
+ Flag.create!(:sneak => options[:by], :flaggeable => self, :report_type => options[:as].to_s)
+ end
+end
View
7 lib/sneak/sneak_methods.rb
@@ -0,0 +1,7 @@
+module SneakMethods
+ def report(options = {})
+ options.assert_valid_keys :on, :as
+
+ Flag.create!(:sneak => self, :flaggeable => options[:on], :report_type => options[:as].to_s)
+ end
+end
View
2 lib/sneak/version.rb
@@ -1,3 +1,3 @@
module Sneak
- VERSION = "0.0.2"
+ VERSION = "0.0.3"
end
View
2 sneak.gemspec
@@ -19,5 +19,5 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]
s.add_development_dependency "rspec"
- s.add_runtime_dependency "haml-rails"
+ s.add_runtime_dependency "rails"
end
View
0 spec/.gitkeep
No changes.

0 comments on commit 5eb4760

Please sign in to comment.