Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

db/seeds/social_stream.yml

  • Loading branch information...
commit 691ddf1073752855839206942e526ba7854d4238 1 parent 3c72e58
@atd atd authored
View
8 README.rdoc
@@ -38,8 +38,12 @@ And run:
rails generate social_stream:install
-This will generate a migration providing the database schema and create a configuration
-file for Social Stream.
+This will generate the following:
+ * A migration providing the database schema
+ * An initializer configuration file for Social Stream.
+ * A database seeds file for defining Social Stream relations. You must add:
+ SocialStream.seed!
+ to your db/seeds.rb
== Actors and Activity Objects
View
4 lib/generators/social_stream/install_generator.rb
@@ -7,6 +7,10 @@ def create_initializer_file
copy_file 'initializer.rb', 'config/initializers/social_stream.rb'
end
+ def create_config_file
+ copy_file 'seeds.yml', 'db/seeds/social_stream.yml'
+ end
+
# TODO: hook_for :orm
require 'rails/generators/active_record'
View
29 lib/generators/social_stream/templates/seeds.yml
@@ -0,0 +1,29 @@
+# Seed configuration for Social Stream
+#
+# Define your relations and add SocialStream.seed! to db/seeds.rb
+#
+# Example
+# relations:
+# user-friend:
+# sender_type: User
+# receiver_type: User
+# name: friend
+# permissions:
+# - [ create, resources, weak_set ]
+# - [ read, resources, group_set ]
+# - [ update, resources, weak_set ]
+# - [ destroy, resources, weak_set ]
+# user-fof:
+# sender_type: User
+# receiver_type: User
+# name: friend_of_friend
+# parent: user-friend
+# permissions:
+# - [ read, resources, group_set ]
+# user-public:
+# sender_type: User
+# receiver_type: User
+# name: public
+# parent: user-fof
+# permissions:
+# - [ read, resources, group_set ]
View
41 lib/social_stream.rb
@@ -11,5 +11,46 @@ class << self
def setup
yield self
end
+
+ def seed!
+ s = YAML.load_file("#{ Rails.root }/db/seeds/social_stream.yml")
+
+ seed_activity_verbs
+ seed_relations(s['relations'])
+ end
+
+ def seed_activity_verbs
+ ActivityVerb::Available.each do |value|
+ ActivityVerb.find_or_create_by_name value
+ end
+ end
+
+ def seed_relations(rs)
+ relations = {}
+
+ rs.each_pair do |name, r|
+ relations[name] =
+ Relation.
+ find_or_create_by_sender_type_and_receiver_type_and_name(r['sender_type'],
+ r['receiver_type'],
+ r['name'])
+ # FIXME: optimize
+ relations[name].relation_permissions.destroy_all
+
+ if (ps = r['permissions']).present?
+ ps.each do |p|
+ relations[name].permissions <<
+ Permission.find_or_create_by_action_and_object_and_parameter(*p)
+ end
+ end
+ end
+
+ # Parent relations must be set after creation
+ rs.each_pair do |name, r|
+ relations[name].parent = relations[r['parent']]
+ relations[name].save!
+ end
+ end
+
end
end
View
51 spec/dummy/config/social_stream.yml
@@ -0,0 +1,51 @@
+relations:
+ user-friend:
+ sender_type: User
+ receiver_type: User
+ name: friend
+ permissions:
+ - [ create, resources, weak_set ]
+ - [ read, resources, group_set ]
+ - [ update, resources, weak_set ]
+ - [ destroy, resources, weak_set ]
+ user-fof:
+ sender_type: User
+ receiver_type: User
+ name: friend_of_friend
+ parent: user-friend
+ permissions:
+ - [ read, resources, group_set ]
+ user-public:
+ sender_type: User
+ receiver_type: User
+ name: public
+ parent: user-fof
+ permissions:
+ - [ read, resources, group_set ]
+
+ user-space-admin:
+ sender_type: User
+ receiver_type: Space
+ name: admin
+ permissions:
+ - [ create, resources, weak_set ]
+ - [ read, resources, group_set ]
+ - [ update, resources, weak_group_set ]
+ - [ destroy, resources, weak_group_set ]
+ user-space-user:
+ sender_type: User
+ receiver_type: Space
+ name: user
+ parent: user-space-admin
+ permissions:
+ - [ create, resources, weak_set ]
+ - [ read, resources, group_set ]
+ - [ update, resources, weak_group_set ]
+ - [ destroy, resources, weak_group_set ]
+ user-space-follower:
+ sender_type: User
+ receiver_type: Space
+ name: follower
+ parent: user-space-user
+ permissions:
+ - [ read, resources, group_set ]
View
60 spec/dummy/db/seeds.rb
@@ -6,62 +6,4 @@
# cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }])
# Mayor.create(:name => 'Daley', :city => cities.first)
-ActivityVerb::Available.each do |value|
- ActivityVerb.find_or_create_by_name value
-end
-
-{ 'User' => {
- 'User' => [
- { 'Friend' => [
- [ 'create' , 'resources', 'weak_set' ],
- [ 'read', 'resources', 'group_set' ],
- [ 'update' , 'resources', 'weak_set' ],
- [ 'destroy', 'resources', 'weak_set' ] ] },
- { 'FriendOfFriend' => [
- [ 'read', 'resources', 'group_set' ] ] },
- { 'Public' => [
- [ 'read', 'resources', 'group_set' ] ] }
- ],
- 'Space' => [
- { 'Admin' => [
- [ 'create' , 'resources', 'weak_set' ],
- [ 'read', 'resources', 'group_set' ],
- [ 'update' , 'resources', 'weak_group_set' ],
- [ 'destroy', 'resources', 'weak_group_set' ] ] },
- { 'User' => [
- [ 'create' , 'resources', 'weak_set' ],
- [ 'read', 'resources', 'group_set' ],
- [ 'update' , 'resources', 'weak_group_set' ],
- [ 'destroy', 'resources', 'weak_group_set' ] ] },
- { 'Follower' => [
- [ 'read', 'resources', 'group_set' ] ] }
- ]
- }
-}.each_pair do |sender_type, receivers|
- receivers.each_pair do |receiver_type, ordered_rs|
- parent_relation = nil
-
- ordered_rs.each do |rs|
- rs.each_pair do |name, ps|
- r =
- Relation.find_by_sender_type_and_receiver_type_and_name(sender_type,
- receiver_type,
- name)
-
- if r.blank?
- r = Relation.create! :name => name,
- :sender_type => sender_type,
- :receiver_type => receiver_type,
- :parent => parent_relation
- end
-
- ps.each do |p|
- p = Permission.find_or_create_by_action_and_object_and_parameter(*p)
- r.permissions << p unless r.permissions.include?(p)
- end
-
- parent_relation = r
- end
- end
- end
-end
+SocialStream.seed!
View
12 spec/factories/tie.rb
@@ -7,15 +7,15 @@
# UserToUser ties
Factory.define :friend_tie, :parent => :tie do |t|
- t.relation { |r| Relation.mode('User', 'User').find_by_name('Friend') }
+ t.relation { |r| Relation.mode('User', 'User').find_by_name('friend') }
end
Factory.define :fof_tie, :parent => :tie do |t|
- t.relation { |r| Relation.mode('User', 'User').find_by_name('FriendOfFriend') }
+ t.relation { |r| Relation.mode('User', 'User').find_by_name('friend_of_friend') }
end
Factory.define :public_tie, :parent => :tie do |t|
- t.relation { |r| Relation.mode('User', 'User').find_by_name('Public') }
+ t.relation { |r| Relation.mode('User', 'User').find_by_name('public') }
end
# UserToSpace ties
@@ -25,14 +25,14 @@
end
Factory.define :admin_tie, :parent => :u2s_tie do |t|
- t.relation { |r| Relation.mode('User', 'Space').find_by_name('Admin') }
+ t.relation { |r| Relation.mode('User', 'Space').find_by_name('admin') }
end
Factory.define :user_tie, :parent => :u2s_tie do |t|
- t.relation { |r| Relation.mode('User', 'Space').find_by_name('User') }
+ t.relation { |r| Relation.mode('User', 'Space').find_by_name('user') }
end
Factory.define :follower_tie, :parent => :u2s_tie do |t|
- t.relation { |r| Relation.mode('User', 'Space').find_by_name('Follower') }
+ t.relation { |r| Relation.mode('User', 'Space').find_by_name('follower') }
end
View
8 spec/models/activity_spec.rb
@@ -133,7 +133,7 @@ def create_ability_accessed_by(tie_type)
describe "belonging to fof tie from a friend" do
before do
- create_activity_assigned_to(Factory(:friend_tie).related('FriendOfFriend'))
+ create_activity_assigned_to(Factory(:friend_tie).related('friend_of_friend'))
end
describe "accessed by a friend" do
@@ -195,7 +195,7 @@ def create_ability_accessed_by(tie_type)
describe "belonging to public tie" do
before do
- create_activity_assigned_to(Factory(:friend_tie).related('Public'))
+ create_activity_assigned_to(Factory(:friend_tie).related('public'))
end
describe "accessed by a friend" do
@@ -307,7 +307,7 @@ def create_ability_accessed_by(tie_type)
describe "belonging to user tie from an admin" do
before do
- create_activity_assigned_to(Factory(:admin_tie).related('User'))
+ create_activity_assigned_to(Factory(:admin_tie).related('user'))
end
describe "accessed by a admin" do
@@ -358,7 +358,7 @@ def create_ability_accessed_by(tie_type)
describe "belonging to follower tie" do
before do
- create_activity_assigned_to(Factory(:user_tie).related('Follower'))
+ create_activity_assigned_to(Factory(:user_tie).related('follower'))
end
describe "accessed by an admin" do
Please sign in to comment.
Something went wrong with that request. Please try again.