acts_as_shareable_object 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +34 -0
  4. data/lib/acts_as_shareable_object.rb +21 -0
  5. data/lib/acts_as_shareable_object/action_view/shareable_object/view_helpers.rb +25 -0
  6. data/lib/acts_as_shareable_object/active_record/acts/shareable_object.rb +47 -0
  7. data/lib/acts_as_shareable_object/active_record/acts/shareable_object/default_properties.rb +79 -0
  8. data/lib/acts_as_shareable_object/version.rb +3 -0
  9. data/lib/tasks/acts_as_shareable_object_tasks.rake +4 -0
  10. data/test/acts_as_shareable_object_test.rb +105 -0
  11. data/test/dummy/README.rdoc +28 -0
  12. data/test/dummy/Rakefile +6 -0
  13. data/test/dummy/app/assets/javascripts/application.js +13 -0
  14. data/test/dummy/app/assets/javascripts/cars.js +2 -0
  15. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  16. data/test/dummy/app/assets/stylesheets/cars.css +4 -0
  17. data/test/dummy/app/controllers/application_controller.rb +5 -0
  18. data/test/dummy/app/controllers/cars_controller.rb +11 -0
  19. data/test/dummy/app/helpers/application_helper.rb +2 -0
  20. data/test/dummy/app/helpers/cars_helper.rb +2 -0
  21. data/test/dummy/app/models/car.rb +3 -0
  22. data/test/dummy/app/views/cars/_car.html.erb +5 -0
  23. data/test/dummy/app/views/cars/index.html.erb +4 -0
  24. data/test/dummy/app/views/cars/show.html.erb +5 -0
  25. data/test/dummy/app/views/layouts/application.html.erb +15 -0
  26. data/test/dummy/bin/bundle +3 -0
  27. data/test/dummy/bin/rails +4 -0
  28. data/test/dummy/bin/rake +4 -0
  29. data/test/dummy/bin/setup +29 -0
  30. data/test/dummy/config.ru +4 -0
  31. data/test/dummy/config/application.rb +26 -0
  32. data/test/dummy/config/boot.rb +5 -0
  33. data/test/dummy/config/database.yml +25 -0
  34. data/test/dummy/config/environment.rb +5 -0
  35. data/test/dummy/config/environments/development.rb +41 -0
  36. data/test/dummy/config/environments/production.rb +79 -0
  37. data/test/dummy/config/environments/test.rb +42 -0
  38. data/test/dummy/config/initializers/assets.rb +11 -0
  39. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  40. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  41. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  42. data/test/dummy/config/initializers/inflections.rb +16 -0
  43. data/test/dummy/config/initializers/mime_types.rb +4 -0
  44. data/test/dummy/config/initializers/session_store.rb +3 -0
  45. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  46. data/test/dummy/config/locales/en.yml +23 -0
  47. data/test/dummy/config/routes.rb +7 -0
  48. data/test/dummy/config/secrets.yml +22 -0
  49. data/test/dummy/db/development.sqlite3 +0 -0
  50. data/test/dummy/db/migrate/20150215104207_create_cars.rb +10 -0
  51. data/test/dummy/db/migrate/20150215145135_add_more_attributes_to_cars.rb +9 -0
  52. data/test/dummy/db/schema.rb +28 -0
  53. data/test/dummy/db/seeds.rb +9 -0
  54. data/test/dummy/db/test.sqlite3 +0 -0
  55. data/test/dummy/log/development.log +8427 -0
  56. data/test/dummy/log/test.log +16004 -0
  57. data/test/dummy/public/404.html +67 -0
  58. data/test/dummy/public/422.html +67 -0
  59. data/test/dummy/public/500.html +66 -0
  60. data/test/dummy/public/favicon.ico +0 -0
  61. data/test/dummy/tmp/cache/assets/development/sprockets/03aeb933f06708e03e52f7f44acc884b +0 -0
  62. data/test/dummy/tmp/cache/assets/development/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  63. data/test/dummy/tmp/cache/assets/development/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  64. data/test/dummy/tmp/cache/assets/development/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  65. data/test/dummy/tmp/cache/assets/development/sprockets/4a15842252a87de8daa19b446f2c6c5b +0 -0
  66. data/test/dummy/tmp/cache/assets/development/sprockets/98ffb8e3755b9894928e07280e7340ff +0 -0
  67. data/test/dummy/tmp/cache/assets/development/sprockets/a21474216f82afdbf04935ef6df2d565 +0 -0
  68. data/test/dummy/tmp/cache/assets/development/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  69. data/test/dummy/tmp/cache/assets/development/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  70. data/test/dummy/tmp/cache/assets/development/sprockets/f37c4cdf45d802c8d2e5cb5eb4a33a88 +0 -0
  71. data/test/dummy/tmp/cache/assets/development/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  72. data/test/dummy/tmp/cache/assets/development/sprockets/fa9193764bd02599156f31e1eef57d5f +0 -0
  73. data/test/dummy/tmp/cache/assets/test/sprockets/03aeb933f06708e03e52f7f44acc884b +0 -0
  74. data/test/dummy/tmp/cache/assets/test/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  75. data/test/dummy/tmp/cache/assets/test/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  76. data/test/dummy/tmp/cache/assets/test/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  77. data/test/dummy/tmp/cache/assets/test/sprockets/4a15842252a87de8daa19b446f2c6c5b +0 -0
  78. data/test/dummy/tmp/cache/assets/test/sprockets/98ffb8e3755b9894928e07280e7340ff +0 -0
  79. data/test/dummy/tmp/cache/assets/test/sprockets/a21474216f82afdbf04935ef6df2d565 +0 -0
  80. data/test/dummy/tmp/cache/assets/test/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  81. data/test/dummy/tmp/cache/assets/test/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  82. data/test/dummy/tmp/cache/assets/test/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  83. data/test/fixtures/cars.yml +8 -0
  84. data/test/test_helper.rb +24 -0
  85. data/test/view_helpers_test.rb +72 -0
  86. metadata +234 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9c21369d78cf3c3aad83743d42a41f028eb093f6
4
+ data.tar.gz: 299b45c7aabe5602b88ae3e73b8add5c3c20cf83
5
+ SHA512:
6
+ metadata.gz: 0281272e9ebe6a2a245d1dde37894e9a5890b5800dd5f2ccd87de01c437483438dcd13fe7063938621ac891f80a4b391c89760daa4019fe6dbd8c2b6d53c2064
7
+ data.tar.gz: 54d7d6b9afdc60312ea0f6949f599fda33cbcd2632ac2072fc893f3c319942c6f6b3ddfab2e1c4b4f38c82e27fcccb467363157a28f170e308ab80cb56f525c7
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Jure Žove
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/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ActsAsShareableObject'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,21 @@
1
+ require "acts_as_shareable_object/active_record/acts/shareable_object"
2
+ require "acts_as_shareable_object/action_view/shareable_object/view_helpers"
3
+
4
+ module ActsAsShareableObject
5
+ if defined?(Rails::Railtie)
6
+ class Railtie < Rails::Railtie
7
+ initializer 'acts_as_shareable_object.include_in_active_record' do
8
+ ActiveSupport.on_load :active_record do
9
+ ActiveRecord::Base.send(:include, ActiveRecord::Acts::ShareableObject)
10
+ end
11
+ end
12
+
13
+ initializer 'acts_as_shareable_object.view_helpers' do
14
+ ActionView::Base.send(:include, ActionView::ShareableObject::ViewHelpers)
15
+ end
16
+ end
17
+ else
18
+ ActiveRecord::Base.send(:include, ActiveRecord::Acts::ShareableObject) if defined?(ActiveRecord)
19
+ ActionView::Base.send(:include, ActionView::ShareableObject::ViewHelpers) if defined?(ActionView)
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ module ActionView
2
+ module ShareableObject
3
+ module ViewHelpers
4
+ def social_meta_tags_for(object, overrides = {})
5
+ raise "You need to add acts_as_shareable_object to your #{object.class} model." unless object.respond_to?(:social_meta_properties)
6
+ parse_tags(object.social_meta_properties).join("\n").html_safe
7
+ end
8
+
9
+ private
10
+
11
+ def parse_tags(properties, namespace = nil)
12
+ tags = []
13
+ Array(properties).each do |key, value|
14
+ if value.is_a?(Hash)
15
+ tags << parse_tags(value, [namespace, key].compact.join(':'))
16
+ else
17
+ name = [namespace, key].join(':')
18
+ tags << tag(:meta, {name: name, property: name, content: value}, true)
19
+ end
20
+ end
21
+ tags
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,47 @@
1
+ require 'acts_as_shareable_object/active_record/acts/shareable_object/default_properties'
2
+
3
+ module ActiveRecord
4
+ module Acts
5
+ module ShareableObject
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+ def acts_as_shareable_object(options = {})
12
+ cattr_accessor :shareable_options
13
+ self.shareable_options = [options].flatten
14
+ # delegate :url_helpers, to: 'Rails.application.routes'
15
+
16
+ include InstanceMethods
17
+ end
18
+ end
19
+
20
+ module InstanceMethods
21
+ def social_meta_properties
22
+ properties = values_for(ActiveRecord::Acts::ShareableObject::DefaultProperties.all)
23
+ properties.merge! values_for(shareable_options) unless shareable_options.nil?
24
+ properties
25
+ end
26
+
27
+ private
28
+
29
+ def values_for(properties, namespace = nil)
30
+ values = {}
31
+ Array(properties).each do |property|
32
+ if property.is_a?(Hash)
33
+ property.map do |k, v|
34
+ method = [namespace, k].compact.join("_")
35
+ values[k] = respond_to?(method) ? send(method) : values_for(v, method)
36
+ end
37
+ else
38
+ method = [namespace, property].compact.join("_")
39
+ values[property] = send(method) if respond_to?(method)
40
+ end
41
+ end
42
+ values.reject{ |k, v| v.empty? }
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,79 @@
1
+ module ActiveRecord
2
+ module Acts
3
+ module ShareableObject
4
+ module DefaultProperties
5
+ TWITTER = [
6
+ twitter: [
7
+ :card,
8
+ :title,
9
+ :description,
10
+ :url,
11
+ :image0,
12
+ :image1,
13
+ :image2,
14
+ :image3,
15
+ :data1,
16
+ :label1,
17
+ :data2,
18
+ :label2,
19
+ image: [
20
+ :src,
21
+ :width,
22
+ :height
23
+ ],
24
+ player: [
25
+ :width,
26
+ :height,
27
+ :stream
28
+ ],
29
+ site: [:id],
30
+ creator: [:id],
31
+ app: [
32
+ name: [
33
+ :iphone,
34
+ :ipad,
35
+ :googleplay
36
+ ],
37
+ id: [
38
+ :iphone,
39
+ :ipad,
40
+ :googleplay
41
+ ],
42
+ url: [
43
+ :iphone,
44
+ :ipad,
45
+ :googleplay
46
+ ]
47
+ ]
48
+ ]
49
+ ]
50
+
51
+ OPEN_GRAPH = [
52
+ og: [
53
+ :title,
54
+ :site_name,
55
+ :url,
56
+ :description,
57
+ :image,
58
+ :type
59
+ ],
60
+ article: [
61
+ :author,
62
+ :publisher
63
+ ]
64
+ ]
65
+
66
+ FACEBOOK = [
67
+ fb: [
68
+ :app_id,
69
+ :admin
70
+ ]
71
+ ]
72
+
73
+ def self.all
74
+ TWITTER | OPEN_GRAPH | FACEBOOK
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsShareableObject
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :acts_as_shareable_object do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,105 @@
1
+ require 'test_helper'
2
+
3
+ class ActsAsShareableObjectTest < ActiveSupport::TestCase
4
+ fixtures :cars
5
+
6
+ test "truth" do
7
+ assert_kind_of Module, ActsAsShareableObject
8
+ end
9
+
10
+ test "twitter summary card type" do
11
+ car = cars(:pagani)
12
+
13
+ car.stubs(:twitter_card).returns("summary")
14
+
15
+ assert_equal car.social_meta_properties[:twitter][:card], "summary"
16
+ end
17
+
18
+ test "twitter summary card image" do
19
+ car = cars(:pagani)
20
+
21
+ car.stubs(:twitter_image).returns(car.image)
22
+
23
+ assert_equal car.social_meta_properties[:twitter][:image], car.image
24
+ end
25
+
26
+ test "twitter card site" do
27
+ car = cars(:pagani)
28
+
29
+ car.stubs(:twitter_site).returns(car.company_twitter_handle)
30
+
31
+ assert_equal car.social_meta_properties[:twitter][:site], car.company_twitter_handle
32
+ end
33
+
34
+ test "twitter photo card image src" do
35
+ car = cars(:pagani)
36
+
37
+ car.stubs(:twitter_image_src).returns(car.image)
38
+
39
+ assert_equal car.social_meta_properties[:twitter][:image][:src], car.image
40
+
41
+ end
42
+
43
+ test "twitter photo card image details" do
44
+ car = cars(:pagani)
45
+
46
+ car.stubs(:twitter_image_src).returns(car.image)
47
+ car.stubs(:twitter_image_width).returns("812")
48
+ car.stubs(:twitter_image_height).returns("475")
49
+
50
+ assert_equal car.social_meta_properties[:twitter][:image][:src], car.image
51
+ assert_equal car.social_meta_properties[:twitter][:image][:width], car.twitter_image_width
52
+ assert_equal car.social_meta_properties[:twitter][:image][:height], car.twitter_image_height
53
+ end
54
+
55
+ test "single custom tag" do
56
+ car = cars(:pagani)
57
+
58
+ class << car
59
+ acts_as_shareable_object :og_test
60
+ end
61
+
62
+ car.stubs(:og_test).returns("test tag, YO")
63
+
64
+ assert_equal car.social_meta_properties[:og_test], car.og_test
65
+ end
66
+
67
+ test "single nested custom tag" do
68
+ car = cars(:pagani)
69
+
70
+ class << car
71
+ acts_as_shareable_object og: :nested
72
+ end
73
+
74
+ car.stubs(:og_nested).returns("this is nested")
75
+
76
+ assert_equal car.social_meta_properties[:og][:nested], car.og_nested
77
+ end
78
+
79
+ test "single nested array custom tag" do
80
+ car = cars(:pagani)
81
+
82
+ class << car
83
+ acts_as_shareable_object og: [:nested]
84
+ end
85
+
86
+ car.stubs(:og_nested).returns("this is nested")
87
+
88
+ assert_equal car.social_meta_properties[:og][:nested], car.og_nested
89
+ end
90
+
91
+ test "multiple custom shareable tags" do
92
+ car = cars(:pagani)
93
+
94
+ class << car
95
+ acts_as_shareable_object og: [:custom_property, nested: [:egg]]
96
+ end
97
+
98
+ car.stubs(:og_custom_property).returns("og custom property")
99
+ car.stubs(:og_nested_egg).returns("nested egg")
100
+
101
+ assert_equal car.social_meta_properties[:og][:custom_property], car.og_custom_property
102
+ assert_equal car.social_meta_properties[:og][:nested][:egg], car.og_nested_egg
103
+ end
104
+
105
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end