Module: PlataformaSocial::Helpers

Included in:
ActionController::Base
Defined in:
lib/plataforma_social/helpers.rb

Instance Method Summary collapse

Instance Method Details

#custom_redirect_to(path, options = {}, protocol = '') ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/plataforma_social/helpers.rb', line 75

def custom_redirect_to path, options = {}, protocol = ''
  options.each_pair do |key, value|
    cookies[key] = value
  end
  
  protocol = "#{protocol}:" if protocol.present?
  
  render :text => %Q{
    <script type="text/javascript" charset="utf-8">
      window.top.location = "#{protocol}//apps.facebook.com/#{PlataformaSocial.facebook_app_namespace}#{path}";
    </script>
  }
end

#from_admin?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/plataforma_social/helpers.rb', line 93

def from_admin?
  params.present? && params[:controller].present? && params[:controller].index(PlataformaSocial.admin_controller_name) == 0
end

#from_facebook?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/plataforma_social/helpers.rb', line 89

def from_facebook?
  request.referer && request.referer.index('facebook.com')
end


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/plataforma_social/helpers.rb', line 97

def link_to *args, &block
  args[2] ||= {}
  
  if args[2][:normal] === true
    super
  else
    unless block_given?
      if from_facebook? && !from_admin?
        server_domain = request.env['HTTP_HOST']
        url           = 'apps.facebook.com/' + PlataformaSocial.facebook_app_namespace 
        
        if args[1].include? server_domain
          args[1].gsub! server_domain, url
        else
          args[1] = request.protocol + url + args[1]
        end
      
        args[2][:target] = '_top'
      else
        super
      end
    end
    
    super
  end
end

#plataforma_social_config_scriptObject



12
13
14
15
16
17
# File 'lib/plataforma_social/helpers.rb', line 12

def plataforma_social_config_script
  %Q{<script type="text/javascript" charset="utf-8">
    $P.init({ apiKey: '#{PlataformaSocial.api_key}' });
    window._FACEBOOK_APP_URL = 'http://apps.facebook.com/#{PlataformaSocial.facebook_app_namespace}';
  </script>}.html_safe
end

#plataforma_social_javascriptsObject



4
5
6
# File 'lib/plataforma_social/helpers.rb', line 4

def plataforma_social_javascripts
  plataforma_social_script_tag + plataforma_social_config_script + signed_request_javascript
end

#plataforma_social_script_tagObject



8
9
10
# File 'lib/plataforma_social/helpers.rb', line 8

def plataforma_social_script_tag
  %Q{<script src="#{PlataformaSocial.domains('js')}/socialp.js" type="text/javascript" charset="utf-8"></script>}.html_safe
end

#signed_request_javascriptObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/plataforma_social/helpers.rb', line 19

def signed_request_javascript
  default_params = {
    :signed_request => params[:signed_request] || nil,
    :code => params[:code] || nil
  }
  
  %Q{
    <script type="text/javascript" charset="utf-8">
      if($ && jQuery){
        $(function(){      
          $(document).ajaxSend(function(elm, xhr, s){
            var method        = s.type;
            var defaultParams = #{default_params.to_json};
            var params        = {};
            var query         = [];
    
            var data = method == "POST" ? s.data : s.url.split('?')[1];
      
            if(data){
              var splittedData = data.split('&');
              var splittedDataLength = splittedData.length;

              if(splittedDataLength){
                for(var i = 0; i < splittedDataLength; i++){
                  var param = splittedData[i].split('=');
        
                  params[param[0]] = param[1];
                } // for
              }
            } // if
    
            for(var i in defaultParams){          
              if(!params[i]) params[i] = defaultParams[i];
            }
          
            for(var i in params){
              query.push(decodeURIComponent(i) + '=' + decodeURIComponent(params[i]));
            }
            
            query = query.join('&');
          
            if(method == "POST"){
              s.data = query;
            }
            else {
              s.url = s.url.split('?')[0] + '?' + query; 
            }
            
            xhr.setRequestHeader("Content-type", s.contentType);
          });
        });          
      }
    </script>
  }.html_safe
end