prixfixe 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,69 @@
1
+ require 'net/http'
2
+
3
+ module PrixFixeModel
4
+ def dump_data
5
+ hash = {}
6
+ hash["Key"] = self[index[0]]
7
+ tokens = {}
8
+ attrs_to_include.each do |attr|
9
+ tokens[attr] = self.send(attr).to_s
10
+ end
11
+ hash["Tokens"] = tokens
12
+ hash.to_json
13
+ end
14
+
15
+ def remove_data
16
+ {
17
+ "Key" => self.send("#{index[0]}_was"),
18
+ "Tokens" => nil
19
+ }.to_json
20
+ end
21
+
22
+ def self.included(base)
23
+ base.class_eval do
24
+ after_create :add_to_search
25
+ before_update :reindex
26
+ before_destroy :remove_from_search
27
+ end
28
+ end
29
+
30
+ def post_data(data)
31
+ uri = URI(PRIX_FIXE[:server] + "/putall")
32
+ # This doesn't retry if it fails, and it also blocked. This should probably
33
+ # support resque or delayedjob if it's installed
34
+ response = Net::HTTP.post_form(uri, {"data" => data})
35
+ end
36
+
37
+ def add_to_search
38
+ json_data = self.dump_data
39
+ post_data(json_data)
40
+ end
41
+
42
+ def reindex
43
+ old_data = self.remove_data
44
+ new_data = self.dump_data
45
+ post_data("#{old_data}\n#{new_data}")
46
+ end
47
+
48
+ def remove_from_search
49
+ post_data(self.remove_data)
50
+ end
51
+ end
52
+
53
+ class Class
54
+ def index_on(attribute)
55
+ class_eval %Q(
56
+ def index
57
+ #{attribute}
58
+ end
59
+ )
60
+ end
61
+
62
+ def include_in_search(attrs)
63
+ class_eval %Q(
64
+ def attrs_to_include
65
+ #{attrs}
66
+ end
67
+ )
68
+ end
69
+ end
@@ -0,0 +1,11 @@
1
+ require 'prixfixe'
2
+ require 'rails'
3
+ module MyPlugin
4
+ class Railtie < Rails::Railtie
5
+ railtie_name :prixfixe
6
+
7
+ rake_tasks do
8
+ load "tasks/prixfixe.rake"
9
+ end
10
+ end
11
+ end
data/lib/prixfixe.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Prixfixe
2
- require 'lib/prixfixe/railtie' if defined?(Rails)
2
+ require 'prixfixe/railtie' if defined?(Rails)
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prixfixe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -17,6 +17,8 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - lib/prix_fixe_model.rb
21
+ - lib/prixfixe/railtie.rb
20
22
  - lib/prixfixe.rb
21
23
  homepage: http://rubygems.org/gems/prixfixe
22
24
  licenses: