sharing_tags 0.0.9 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +94 -0
- data/.travis.yml +1 -0
- data/Gemfile +2 -1
- data/Guardfile +21 -11
- data/README.md +8 -1
- data/Rakefile +16 -0
- data/Todo.md +4 -1
- data/app/assets/images/sharing_tags/icons/line.svg +9 -9
- data/app/assets/javascripts/sharing_tags.js +10 -0
- data/app/assets/javascripts/sharing_tags/init.js.coffee.erb +5 -4
- data/app/assets/javascripts/sharing_tags/links.js.coffee +9 -7
- data/app/assets/javascripts/sharing_tags/share.js.coffee +82 -62
- data/app/assets/javascripts/sharing_tags/share/callback.js.coffee +14 -0
- data/app/assets/javascripts/sharing_tags/share/facebook.js.coffee +82 -0
- data/app/assets/stylesheets/sharing_tags/wave_animation.css.sass +33 -0
- data/app/views/sharing_tags/meta/_open_graphs.html.slim +1 -1
- data/app/views/sharing_tags/meta/_twitter_card.html.slim +12 -9
- data/circle.yml +9 -0
- data/lib/generators/sharing_tags/install/install_generator.rb +12 -14
- data/lib/generators/sharing_tags/install/templates/initializer.rb +3 -0
- data/lib/sharing_tags.rb +0 -5
- data/lib/sharing_tags/action_controller/filters.rb +13 -10
- data/lib/sharing_tags/action_controller/helpers.rb +11 -9
- data/lib/sharing_tags/action_view/asset_helper.rb +39 -35
- data/lib/sharing_tags/action_view/button_helper.rb +62 -53
- data/lib/sharing_tags/action_view/meta_helper.rb +11 -8
- data/lib/sharing_tags/config.rb +3 -4
- data/lib/sharing_tags/configuration.rb +17 -18
- data/lib/sharing_tags/context.rb +11 -14
- data/lib/sharing_tags/engine.rb +11 -0
- data/lib/sharing_tags/network.rb +64 -30
- data/lib/sharing_tags/network/facebook.rb +11 -8
- data/lib/sharing_tags/railtie.rb +6 -9
- data/lib/sharing_tags/version.rb +1 -1
- data/sharing_tags.gemspec +1 -0
- data/spec/controllers/main_controller_spec.rb +2 -2
- data/spec/dummy/app/helpers/application_helper.rb +1 -0
- data/spec/generators/install_generator_spec.rb +8 -6
- data/spec/helpers/asset_helper_spec.rb +3 -2
- data/spec/helpers/button_helper_spec.rb +70 -76
- data/spec/helpers/meta_tags_helper_spec.rb +1 -2
- data/spec/javascripts/fixtures/facebook.json +3 -1
- data/spec/javascripts/sharing_tags/share/{facebook_share_spec.coffee → facebook_spec.coffee} +73 -12
- data/spec/javascripts/sharing_tags/share/vkontakte_spec.coffee +29 -0
- data/spec/javascripts/sharing_tags/share_spec.coffee +193 -0
- data/spec/javascripts/sharing_tags/sharing_tags_spec.coffee +9 -14
- data/spec/models/configuration_spec.rb +12 -14
- data/spec/models/network_spec.rb +1 -4
- data/spec/models/running_in_context_spec.rb +19 -6
- data/spec/spec_helper.rb +1 -2
- data/spec/support/generator_support.rb +1 -3
- data/spec/teaspoon_env.rb +39 -39
- data/spec/views/meta_tags/facebook_meta_tags_erb_spec.rb +16 -17
- data/spec/views/meta_tags/meta_tags_erb_spec.rb +5 -7
- metadata +29 -13
- data/app/assets/javascripts/sharing_tags.js.coffee +0 -9
- data/app/assets/javascripts/sharing_tags/share/base.js.coffee +0 -38
- data/app/assets/javascripts/sharing_tags/share/facebook.coffee +0 -57
- data/app/assets/javascripts/sharing_tags/share/vkontakte.js.coffee +0 -13
- data/bin/rspec +0 -16
- data/spec/javascripts/sharing_tags/share/vkontakte_share_spec.coffee +0 -31
@@ -1,10 +1,13 @@
|
|
1
|
-
module SharingTags
|
1
|
+
module SharingTags
|
2
|
+
module ActionView
|
3
|
+
module MetaHelper
|
4
|
+
def sharing_tags
|
5
|
+
SharingTags.config.within_context_params(self)
|
6
|
+
end
|
2
7
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
render template: "sharing_tags/meta_tags.html.slim"
|
9
|
-
end
|
8
|
+
def render_sharing_tags
|
9
|
+
render template: "sharing_tags/meta_tags.html.slim"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
10
13
|
end
|
data/lib/sharing_tags/config.rb
CHANGED
@@ -2,10 +2,9 @@ require "hashie/mash"
|
|
2
2
|
|
3
3
|
module SharingTags
|
4
4
|
class Config < ::Hashie::Mash
|
5
|
+
# TODO: initialize default param as Config.new
|
5
6
|
|
6
|
-
#
|
7
|
-
|
8
|
-
# note: temporary code for working construction sharing_tags.switch_context_to
|
7
|
+
# NOTE: temporary code for working construction sharing_tags.switch_context_to
|
9
8
|
def switch_context_to(name, *attrs)
|
10
9
|
Rails.logger.debug "SharingTags: switch context from #{SharingTags.config.current_context.name} to #{name}"
|
11
10
|
SharingTags.config.switch_context(name, *attrs)
|
@@ -17,7 +16,7 @@ module SharingTags
|
|
17
16
|
lists.each do |divide_key|
|
18
17
|
second_part[divide_key] =
|
19
18
|
if self.key?(divide_key)
|
20
|
-
|
19
|
+
delete(divide_key)
|
21
20
|
else
|
22
21
|
self.class.new
|
23
22
|
end
|
@@ -1,32 +1,36 @@
|
|
1
1
|
module SharingTags
|
2
2
|
class Configuration
|
3
|
-
|
4
|
-
NETWORKS = %i{ google facebook twitter }
|
3
|
+
NETWORKS = %i( google facebook twitter )
|
5
4
|
|
6
5
|
attr_accessor :running_context
|
6
|
+
attr_reader :default_context
|
7
7
|
|
8
8
|
def initialize
|
9
9
|
clear!
|
10
10
|
end
|
11
11
|
|
12
12
|
def context(name, &block)
|
13
|
-
|
13
|
+
fail "please define context block params" unless block_given?
|
14
14
|
(@contexts[name] ||= Context.new(name, self)).instance_exec(&block)
|
15
15
|
end
|
16
16
|
|
17
|
-
def switch_context(name = nil, *args)
|
17
|
+
def switch_context(name = nil, *args, &block)
|
18
18
|
clean_params!
|
19
|
+
prev_context = current_context
|
20
|
+
prev_context_params = @current_context_params
|
21
|
+
|
19
22
|
@current_context_params = args
|
20
|
-
@
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
23
|
+
@current_context = name && @contexts[name] || default_context
|
24
|
+
return unless block_given?
|
25
|
+
|
26
|
+
result = block.call
|
27
|
+
|
28
|
+
@current_context = prev_context
|
29
|
+
@current_context_params = prev_context_params
|
30
|
+
|
31
|
+
result
|
28
32
|
end
|
29
|
-
|
33
|
+
alias_method :switch_context_to, :switch_context
|
30
34
|
|
31
35
|
def clear!
|
32
36
|
@contexts = {}
|
@@ -69,10 +73,5 @@ module SharingTags
|
|
69
73
|
def method_missing(method_name, *arguments, &block)
|
70
74
|
current_context.send(method_name, *arguments, &block)
|
71
75
|
end
|
72
|
-
|
73
|
-
def default_context
|
74
|
-
@default_context
|
75
|
-
end
|
76
|
-
|
77
76
|
end
|
78
77
|
end
|
data/lib/sharing_tags/context.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
module SharingTags
|
2
2
|
class Context
|
3
|
-
|
4
3
|
attr_reader :name
|
5
4
|
attr_reader :configuraton
|
6
5
|
|
@@ -9,8 +8,8 @@ module SharingTags
|
|
9
8
|
@networks = {}
|
10
9
|
@configuraton = configuraton
|
11
10
|
|
12
|
-
Network.lists.each do |
|
13
|
-
|
11
|
+
Network.lists.each do |network_name|
|
12
|
+
send(network_name)
|
14
13
|
end
|
15
14
|
end
|
16
15
|
|
@@ -19,43 +18,43 @@ module SharingTags
|
|
19
18
|
end
|
20
19
|
|
21
20
|
def twitter(&block)
|
22
|
-
(@networks[:twitter] ||=
|
21
|
+
(@networks[:twitter] ||= Network.new(:twitter, self)).tap do |twitter|
|
23
22
|
twitter.instance_exec(&block) if block_given?
|
24
23
|
end
|
25
24
|
end
|
26
25
|
|
27
26
|
def facebook(&block)
|
28
|
-
(@networks[:facebook] ||=
|
27
|
+
(@networks[:facebook] ||= Network::Facebook.new(:facebook, self)).tap do |facebook|
|
29
28
|
facebook.instance_exec(&block) if block_given?
|
30
29
|
end
|
31
30
|
end
|
32
31
|
|
33
32
|
def google(&block)
|
34
|
-
(@networks[:google] ||=
|
33
|
+
(@networks[:google] ||= Network.new(:google, self)).tap do |google|
|
35
34
|
google.instance_exec(&block) if block_given?
|
36
35
|
end
|
37
36
|
end
|
38
37
|
|
39
38
|
def vkontakte(&block)
|
40
|
-
(@networks[:vkontakte] ||=
|
39
|
+
(@networks[:vkontakte] ||= Network.new(:vkontakte, self)).tap do |vkontakte|
|
41
40
|
vkontakte.instance_exec(&block) if block_given?
|
42
41
|
end
|
43
42
|
end
|
44
43
|
|
45
44
|
def line(&block)
|
46
|
-
(@networks[:line] ||=
|
45
|
+
(@networks[:line] ||= Network.new(:line, self)).tap do |line|
|
47
46
|
line.instance_exec(&block) if block_given?
|
48
47
|
end
|
49
48
|
end
|
50
49
|
|
51
50
|
def odnoklassniki(&block)
|
52
|
-
(@networks[:odnoklassniki] ||=
|
51
|
+
(@networks[:odnoklassniki] ||= Network.new(:odnoklassniki, self)).tap do |odnoklassniki|
|
53
52
|
odnoklassniki.instance_exec(&block) if block_given?
|
54
53
|
end
|
55
54
|
end
|
56
55
|
|
57
56
|
def linkedin(&block)
|
58
|
-
(@networks[:linkedin] ||=
|
57
|
+
(@networks[:linkedin] ||= Network.new(:linkedin, self)).tap do |vkontakte|
|
59
58
|
vkontakte.instance_exec(&block) if block_given?
|
60
59
|
end
|
61
60
|
end
|
@@ -73,14 +72,13 @@ module SharingTags
|
|
73
72
|
def fetch_params(context_args = nil, default_config_params = Config.new)
|
74
73
|
default_context_params = fetch_default_context_params(context_args, default_config_params || Config.new)
|
75
74
|
|
76
|
-
@networks.
|
75
|
+
@networks.each_with_object(Config.new) do |(name, network), result|
|
77
76
|
param = (result[name] ||= Config.new)
|
78
77
|
|
79
78
|
default_network_params = default_context_params[name]
|
80
79
|
param.deep_update network.attributes_for(context_args, default_network_params)
|
81
80
|
|
82
81
|
result[name] = param
|
83
|
-
result
|
84
82
|
end
|
85
83
|
end
|
86
84
|
|
@@ -99,10 +97,9 @@ module SharingTags
|
|
99
97
|
|
100
98
|
def method_missing(method_name, *arguments, &block)
|
101
99
|
unless default_network.class.available_attributes.include?(method_name.to_sym)
|
102
|
-
|
100
|
+
fail Network::Error, "Error didn't find #{method_name} attribute in network"
|
103
101
|
end
|
104
102
|
default_network.send(method_name, *arguments, &block)
|
105
103
|
end
|
106
|
-
|
107
104
|
end
|
108
105
|
end
|
data/lib/sharing_tags/engine.rb
CHANGED
@@ -2,5 +2,16 @@ module SharingTags
|
|
2
2
|
class Engine < ::Rails::Engine
|
3
3
|
isolate_namespace SharingTags
|
4
4
|
|
5
|
+
if Rails.env.development?
|
6
|
+
config.to_prepare do
|
7
|
+
require_dependency Rails.root.join('config', 'initializers', 'sharing_tags.rb').to_s
|
8
|
+
end
|
9
|
+
|
10
|
+
config.after_initialize do
|
11
|
+
# optional, without it will call `to_prepend` only when a file changes,
|
12
|
+
# not on every request
|
13
|
+
Rails.application.config.reload_classes_only_on_change = false
|
14
|
+
end
|
15
|
+
end
|
5
16
|
end
|
6
17
|
end
|
data/lib/sharing_tags/network.rb
CHANGED
@@ -1,15 +1,38 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
1
3
|
module SharingTags
|
2
4
|
class Network
|
3
|
-
|
4
|
-
# todo: add default values
|
5
|
+
# TODO: add default values
|
5
6
|
|
6
7
|
class Error < StandardError
|
7
8
|
end
|
8
9
|
|
10
|
+
class NetworkRunningContext
|
11
|
+
def initialize(network, context)
|
12
|
+
@network_name = network.name
|
13
|
+
@context = context
|
14
|
+
@context_name = @context.name
|
15
|
+
end
|
16
|
+
|
17
|
+
def network
|
18
|
+
@network_name
|
19
|
+
end
|
20
|
+
|
21
|
+
def context
|
22
|
+
@context_name
|
23
|
+
end
|
24
|
+
|
25
|
+
def method_missing(method_name, *arguments, &block)
|
26
|
+
return unless @context && @context.configuraton
|
27
|
+
@context.configuraton.running_context.send(method_name, *arguments, &block)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
9
31
|
NETWORKS = %i( facebook google twitter vkontakte odnoklassniki line linkedin )
|
10
32
|
|
11
|
-
ATTRIBUTES =
|
12
|
-
|
33
|
+
ATTRIBUTES =
|
34
|
+
%i( share_url title description page_url share_url_params link_params
|
35
|
+
image_url image digested_image digested_image_url )
|
13
36
|
|
14
37
|
attr_reader :name, :attributes
|
15
38
|
|
@@ -22,12 +45,12 @@ module SharingTags
|
|
22
45
|
def initialize(name, context = nil)
|
23
46
|
@name = name
|
24
47
|
@context = context
|
48
|
+
@running_context = NetworkRunningContext.new(self, context)
|
25
49
|
clear!
|
26
50
|
end
|
27
51
|
|
28
52
|
def clear!
|
29
53
|
@attributes = {}
|
30
|
-
@share_url_params = nil
|
31
54
|
end
|
32
55
|
|
33
56
|
def self.available_attributes
|
@@ -46,16 +69,16 @@ module SharingTags
|
|
46
69
|
attributes[:description] = store_value(value, &block)
|
47
70
|
end
|
48
71
|
|
49
|
-
#
|
72
|
+
# TODO: activate rubycop Metrics
|
73
|
+
# rubocop:disable Metrics/AbcSize
|
74
|
+
# image_url(new_image = nil, size = nil, content_type = nil, options, &block)
|
50
75
|
def image_url(*arguments, &block)
|
51
76
|
options = arguments.extract_options!
|
52
77
|
new_image, size, content_type = arguments
|
53
78
|
|
54
|
-
if options[:digested] == false &&
|
55
|
-
block = proc { without_digest_asset_url(new_image) }
|
56
|
-
end
|
79
|
+
block = proc { without_digest_asset_url(new_image) } if options[:digested] == false && block_given? == false
|
57
80
|
|
58
|
-
#
|
81
|
+
# TODO: add another class for storing image
|
59
82
|
attributes[:image] = store_value(new_image, &block)
|
60
83
|
|
61
84
|
# add size and content type for block value
|
@@ -64,41 +87,42 @@ module SharingTags
|
|
64
87
|
attributes[:image_size] = store_value(size.split("x").map(&:to_i)) if size
|
65
88
|
attributes[:image_content_type] = store_value(content_type) if content_type
|
66
89
|
end
|
67
|
-
|
90
|
+
alias_method :image, :image_url
|
91
|
+
# rubocop:enable Metrics/AbcSize
|
68
92
|
|
69
|
-
#
|
70
|
-
#
|
93
|
+
# TODO: add image_size
|
94
|
+
# TODO: add_image_type
|
71
95
|
|
72
96
|
def digested_image_url(*arguments, &block)
|
73
97
|
options = arguments.extract_options!
|
74
|
-
options.merge!(:
|
98
|
+
options.merge!(digested: false)
|
75
99
|
|
76
100
|
wrap_block = proc { |*args| without_digest_asset_url(block.call(*args)) } if block_given?
|
77
101
|
image_url(*arguments, options, &wrap_block)
|
78
102
|
end
|
79
|
-
|
103
|
+
alias_method :digested_image, :digested_image_url
|
80
104
|
|
81
105
|
def page_url(new_url = nil, &block)
|
82
106
|
attributes[:page_url] = store_value(new_url, &block)
|
83
107
|
end
|
84
108
|
|
85
109
|
def share_url_params(params = nil, &block)
|
86
|
-
|
110
|
+
attributes[:share_url_params] = store_value(params, &block)
|
87
111
|
end
|
88
|
-
|
112
|
+
alias_method :link_params, :share_url_params
|
89
113
|
|
90
114
|
def attributes_for(context_params = nil, default_params = Config.new)
|
91
|
-
#
|
92
|
-
@attributes.
|
93
|
-
result[
|
94
|
-
result
|
95
|
-
end.tap do |attrs|
|
96
|
-
#todo: fix assign share_url from page_url
|
97
|
-
attrs[:share_url] = attrs[:page_url].dup if !attrs[:share_url] && attrs[:page_url]
|
98
|
-
attrs[:share_url] = ("#{attrs[:share_url]}?" + @share_url_params.to_query) if attrs[:share_url] && @share_url_params
|
99
|
-
|
100
|
-
attrs[:network] = name if attrs.present?
|
115
|
+
# TODO: merge default params after get all values of attributes
|
116
|
+
attrs = @attributes.each_with_object(default_params.dup) do |(a_name, value), result|
|
117
|
+
result[a_name] = get_value(value, context_params)
|
101
118
|
end
|
119
|
+
|
120
|
+
# TODO: fix assign share_url from page_url
|
121
|
+
attrs[:share_url] = attrs[:page_url].dup if !attrs[:share_url] && attrs[:page_url]
|
122
|
+
attrs[:share_url] = add_params_to_url(attrs[:share_url], attrs[:share_url_params]) if attrs[:share_url] && attrs[:share_url_params].present?
|
123
|
+
attrs[:network] = name if attrs.present?
|
124
|
+
|
125
|
+
attrs
|
102
126
|
end
|
103
127
|
|
104
128
|
protected
|
@@ -113,10 +137,9 @@ module SharingTags
|
|
113
137
|
|
114
138
|
def get_value(value, context_params)
|
115
139
|
if value.is_a?(Proc)
|
116
|
-
|
117
|
-
if @context && running_context = @context.configuraton.running_context
|
140
|
+
if @context && @running_context
|
118
141
|
# execute proc within the view context with context_params
|
119
|
-
running_context.instance_exec(*context_params, &value)
|
142
|
+
@running_context.instance_exec(*context_params, &value)
|
120
143
|
else
|
121
144
|
value.call(context_params)
|
122
145
|
end
|
@@ -124,5 +147,16 @@ module SharingTags
|
|
124
147
|
value
|
125
148
|
end
|
126
149
|
end
|
150
|
+
|
151
|
+
def add_params_to_url(url, params = {})
|
152
|
+
uri = URI.parse(url)
|
153
|
+
new_query_array = URI.decode_www_form(uri.query || '') + params.to_a
|
154
|
+
uri.query = URI.encode_www_form(new_query_array)
|
155
|
+
|
156
|
+
uri.to_s.html_safe
|
157
|
+
rescue URI::Error
|
158
|
+
# TODO: raise error
|
159
|
+
url
|
160
|
+
end
|
127
161
|
end
|
128
162
|
end
|
@@ -1,14 +1,17 @@
|
|
1
1
|
module SharingTags
|
2
|
-
class Network
|
2
|
+
class Network
|
3
|
+
class Facebook < Network
|
4
|
+
def self.available_attributes
|
5
|
+
super + %i( app_id provider )
|
6
|
+
end
|
3
7
|
|
4
|
-
|
5
|
-
|
6
|
-
|
8
|
+
def provider(provider = 'auto', &block)
|
9
|
+
attributes[:provider] = store_value(provider, &block)
|
10
|
+
end
|
7
11
|
|
8
|
-
|
9
|
-
|
12
|
+
def app_id(app_id = nil, &block)
|
13
|
+
attributes[:app_id] = store_value(app_id, &block)
|
14
|
+
end
|
10
15
|
end
|
11
|
-
|
12
16
|
end
|
13
|
-
|
14
17
|
end
|
data/lib/sharing_tags/railtie.rb
CHANGED
@@ -1,17 +1,11 @@
|
|
1
1
|
module SharingTags
|
2
|
-
|
3
|
-
|
4
|
-
# config.eager_load_namespaces << SharingTags
|
5
|
-
|
2
|
+
# = Sharing Tags Railtie
|
3
|
+
class Railtie < Rails::Railtie # :nodoc:
|
6
4
|
generators do
|
7
5
|
require "generators/sharing_tags/install/install_generator"
|
8
6
|
end
|
9
7
|
|
10
|
-
|
11
|
-
# load "rspec/rails/tasks/rspec.rake"
|
12
|
-
# end
|
13
|
-
|
14
|
-
initializer "sharing_tags.configure_view_controller" do |app|
|
8
|
+
initializer "sharing_tags.configure_view_controller" do
|
15
9
|
ActiveSupport.on_load :action_view do
|
16
10
|
include SharingTags::ActionView::MetaHelper
|
17
11
|
include SharingTags::ActionView::ButtonHelper
|
@@ -27,5 +21,8 @@ module SharingTags
|
|
27
21
|
end
|
28
22
|
end
|
29
23
|
|
24
|
+
console do
|
25
|
+
ApplicationController.new.view_context
|
26
|
+
end
|
30
27
|
end
|
31
28
|
end
|
data/lib/sharing_tags/version.rb
CHANGED
data/sharing_tags.gemspec
CHANGED
@@ -39,6 +39,7 @@ Gem::Specification.new do |spec|
|
|
39
39
|
spec.add_development_dependency 'guard', '~> 2.12'
|
40
40
|
spec.add_development_dependency 'guard-rspec', '~> 4.5'
|
41
41
|
spec.add_development_dependency 'guard-teaspoon', '~> 0.8'
|
42
|
+
spec.add_development_dependency 'guard-rubocop', '>= 1.2.0'
|
42
43
|
spec.add_development_dependency 'sqlite3', '~> 1.3'
|
43
44
|
spec.add_development_dependency 'pry', '~> 0.10'
|
44
45
|
|