flex-rails 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012-2013 by Domizio Demichelis
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/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # Flex-rails
2
+
3
+ Rails integration for Flex: provides the engine, generators and helpers to integrate Flex with Rails
4
+
5
+ ## Links
6
+
7
+ * [Flex Repository](https://github.com/ddnexus/flex)
8
+ * [Flex Project (Global Documentation)](http://ddnexus.github.io/flex/doc/)
9
+ * [flex-rails Gem (Specific Documentation)](http://ddnexus.github.io/flex/doc/5-flex-rails)
10
+ * [Issues](https://github.com/ddnexus/flex-rails/issues)
11
+ * [Pull Requests](https://github.com/ddnexus/flex-rails/pulls)
12
+
13
+ ## Branches
14
+
15
+ The master branch reflects the last published gem. Then you may find a next-version branch (named after the version string), with the commits that will be merged in master just before publishing the next gem version. The next-version branch may get rebased or force pushed.
16
+
17
+ ## Credits
18
+
19
+ Special thanks for their sponsorship to [Escalate Media](http://www.escalatemedia.com) and [Barquin International](http://www.barquin.com).
20
+
21
+ ## Copyright
22
+
23
+ Copyright (c) 2012-2013 by [Domizio Demichelis](mailto://dd.nexus@gmail.com)<br>
24
+ See [LICENSE](https://github.com/ddnexus/flex-rails/blob/master/LICENSE) for details.
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.1
@@ -0,0 +1,24 @@
1
+ require 'date'
2
+ version = File.read(File.expand_path('../VERSION', __FILE__)).strip
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'flex-rails'
6
+ s.summary = 'Rails integration for Flex'
7
+ s.description = 'Provides the engine, generators and helpers to integrate Flex with Rails'
8
+ s.homepage = 'http://github.com/ddnexus/flex-rails'
9
+ s.authors = ["Domizio Demichelis"]
10
+ s.email = 'dd.nexus@gmail.com'
11
+ s.extra_rdoc_files = %w[README.md]
12
+ s.files = `git ls-files -z`.split("\0")
13
+ s.version = version
14
+ s.date = Date.today.to_s
15
+ s.required_rubygems_version = ">= 1.3.6"
16
+ s.rdoc_options = %w[--charset=UTF-8]
17
+
18
+ s.add_runtime_dependency 'rails', '>=2.0'
19
+
20
+ s.add_runtime_dependency 'flex', version
21
+ s.add_runtime_dependency 'flex-models', version
22
+
23
+ s.add_runtime_dependency 'prompter', '~> 0.1.5'
24
+ end
@@ -0,0 +1,39 @@
1
+ module Flex
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+
5
+ ActiveSupport.on_load(:before_configuration) do
6
+ config.flex = Conf
7
+ config.flex.variables[:index] = [self.class.name.split('::').first.underscore, ::Rails.env].join('_')
8
+ config.flex.config_file = ::Rails.root.join('config', 'flex.yml').to_s
9
+ config.flex.flex_dir = ::Rails.root.join('app', 'flex').to_s
10
+ config.flex.logger = Logger.new(STDOUT)
11
+ config.flex.logger.level = ::Logger::DEBUG if ::Rails.env.development?
12
+ config.flex.result_extenders |= [ Flex::Result::RailsHelper ]
13
+ end
14
+
15
+ ActiveSupport.on_load(:after_initialize) do
16
+ Helper.after_initialize
17
+ end
18
+
19
+ rake_tasks do
20
+ Flex::LIB_PATHS.each do |path|
21
+ task_path = "#{path}/tasks.rake"
22
+ load task_path if File.file?(task_path)
23
+ end
24
+ end
25
+
26
+ console do
27
+ config.flex.logger.log_to_rails_logger = false
28
+ config.flex.logger.log_to_stderr = true
29
+ config.flex.logger.debug_variables = false
30
+ config.flex.logger.debug_result = false
31
+ end
32
+
33
+ config.to_prepare do
34
+ Flex.reload!
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,14 @@
1
+ module Flex
2
+ module Rails
3
+ module Helper
4
+ extend self
5
+
6
+ def after_initialize
7
+ # we need to reload the flex API methods with the new variables
8
+ Flex.reload!
9
+ Conf.flex_models && Conf.flex_models.each {|m| eval"::#{m}" if m.is_a?(String) }
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,29 @@
1
+ module Flex
2
+ module Rails
3
+ class Logger < Flex::Logger
4
+
5
+ attr_accessor :log_to_rails_logger, :log_to_stderr
6
+
7
+ def initialize(*)
8
+ super
9
+ self.formatter = proc do |severity, datetime, progname, msg|
10
+ flex_formatted = flex_format(severity, msg)
11
+ ::Rails.logger.send(severity.downcase.to_sym, flex_formatted) if log_to_rails_logger && ::Rails.logger.respond_to?(severity.downcase.to_sym)
12
+ flex_formatted if log_to_stderr
13
+ end
14
+ @log_to_rails_logger = true
15
+ @log_to_stderr = false
16
+ end
17
+
18
+ def log_to_stdout
19
+ Deprecation.warn 'Flex::Configuration.logger.log_to_stdout', 'Flex::Configuration.logger.log_to_stderr'
20
+ log_to_stderr
21
+ end
22
+ def log_to_stdout=(val)
23
+ Deprecation.warn 'Flex::Configuration.logger.log_to_stdout=', 'Flex::Configuration.logger.log_to_stderr='
24
+ self.log_to_stderr = val
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,48 @@
1
+ module Flex
2
+ class Result
3
+ module RailsHelper
4
+
5
+ module Highlighter
6
+
7
+ RE = /highlighted_(\w+)/
8
+
9
+ def respond_to?(meth, private=false)
10
+ meth.to_s =~ RE
11
+ !!$1 || super
12
+ end
13
+
14
+ def method_missing(meth, *args, &block)
15
+ meth.to_s =~ RE
16
+ attribute = $1
17
+ if attribute
18
+ opts = {:fragment_separator => ' ... '}.merge(args.first||{})
19
+ if self['highlight']
20
+ key, high = self['highlight'].find { |k,v| k.gsub('.','_') == attribute }
21
+ high = Array.wrap(high) if high
22
+ end
23
+ if high.blank?
24
+ respond_to?(attribute.to_sym) ? send(attribute.to_sym) : ''
25
+ else
26
+ high.join(opts[:fragment_separator]).html_safe
27
+ end
28
+ else
29
+ super
30
+ end
31
+ end
32
+
33
+ end
34
+
35
+
36
+ # extend if result is a Search or MultiGet
37
+ def self.should_extend?(result)
38
+ result.is_a?(Search) || result.is_a?(MultiGet)
39
+ end
40
+
41
+ # extend the collection on extend
42
+ def self.extended(result)
43
+ result.collection.each { |h| h.extend(Highlighter) }
44
+ end
45
+
46
+ end
47
+ end
48
+ end
data/lib/flex-rails.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'flex'
2
+ require 'flex-models'
3
+ require 'rails'
4
+ require 'flex/result/rails_helper'
5
+ require 'flex/rails/helper'
6
+ require 'flex/rails/logger'
7
+
8
+ Flex::LIB_PATHS << File.dirname(__FILE__)
9
+
10
+ if ::Rails.respond_to?(:version) && ::Rails.version.to_i >= 3
11
+ require 'flex/rails/engine'
12
+ else
13
+ Flex::Conf.configure do |c|
14
+ c.config_file = "#{RAILS_ROOT}/config/flex.yml"
15
+ c.flex_dir = "#{RAILS_ROOT}app/flex"
16
+ c.logger = Logger.new(STDOUT)
17
+ c.logger.level = ::Logger::DEBUG if RAILS_ENV == 'development'
18
+ c.result_extenders |= [ Flex::Result::RailsHelper ]
19
+ end
20
+ end
@@ -0,0 +1,53 @@
1
+ require 'prompter'
2
+
3
+ class Flex::SetupGenerator < Rails::Generators::Base
4
+
5
+ source_root File.expand_path('../templates', __FILE__)
6
+
7
+ def self.banner
8
+ "rails generate flex:setup"
9
+ end
10
+
11
+ def ask_base_name
12
+ @module_name = Prompter.ask('Please, enter a class name for your Search class. Choose a name not defined in your app.',
13
+ :default => 'FlexSearch', :hint => '[<enter>=FlexSearch]')
14
+ @extender_name = "#{@module_name}Extender"
15
+ end
16
+
17
+ def add_config_flex_file
18
+ template 'flex_config.yml', Rails.root.join('config', 'flex.yml')
19
+ end
20
+
21
+ def create_initializer_file
22
+ template 'flex_initializer.rb.erb', Rails.root.join('config', 'initializers', 'flex.rb')
23
+ end
24
+
25
+ def create_flex_dir
26
+ template 'flex_dir/es.rb.erb', Rails.root.join('app', 'flex', "#{@module_name.underscore}.rb")
27
+ template 'flex_dir/es.yml.erb', Rails.root.join('app', 'flex', "#{@module_name.underscore}.yml")
28
+ template 'flex_dir/es_extender.rb.erb', Rails.root.join('app', 'flex', "#{@extender_name.underscore}.rb")
29
+ end
30
+
31
+
32
+ def show_setup_message
33
+ Prompter.say <<-text, :style => :green
34
+
35
+ Setup done!
36
+
37
+ During prototyping, remember also:
38
+
39
+ 1. each time you include a new Flex::ModelIndexer
40
+ you should add its name to the config.flex_model in "config/initializers/flex.rb"
41
+
42
+ 2. each time you include a new Flex::ActiveModel
43
+ you should add its name to the config.flex_active_model in "config/initializers/flex.rb"
44
+
45
+ 3. each time you add/change a flex.parent relation you should reindex
46
+
47
+ The complete documentation is available at https://github.com/ddnexus/flex-doc/doc
48
+ If you have any problem with Flex, please report the issue at https://github.com/ddnexus/flex/issues.
49
+ text
50
+ end
51
+
52
+ end
53
+
@@ -0,0 +1,18 @@
1
+ # ANCHORS literal key: it will not be used as template
2
+ # you can store here fragments of structures to reuse below
3
+ ANCHORS:
4
+ -
5
+
6
+ # This is a dynamic index name.
7
+ # The default index name generated by Flex is usually <application_name>_<environment>,
8
+ # but you may have changed it in the initializers/flex.rb or you can hardcode it if you prefer.
9
+ # The settings and mapping below will work with any index (providing that it is a single index).
10
+ # If you set it as an array of indices, you must edit this file accordingly, providing one entry per index.
11
+ <%%= Flex::Configuration.variables[:index] %>:
12
+
13
+ settings:
14
+ number_of_shards: 5
15
+ number_of_replicas: 1
16
+
17
+ # add your custom mappings here
18
+ mappings: {}
@@ -0,0 +1,23 @@
1
+ # inspect the methods loaded in this module and their usage
2
+ # in the rails console by just typing:
3
+ # >> puts <%= @module_name %>.flex.doc
4
+ # you can eventually restrict the doc to a single method by pasing its name:
5
+ # >> puts <%= @module_name %>.flex.doc :search
6
+ # see the detailed doc for this feature at http://ddnexus.github.io/flex/doc/2-flex/4-Self-Documentation.html
7
+
8
+ module <%= @module_name %>
9
+
10
+ extend self
11
+
12
+ # if you don't use flex scopes and you stick with flex templates, you can comment the next line
13
+ include Flex::Scopes
14
+
15
+ # enables the templating
16
+ include Flex::Templates
17
+ # loads the <%= "#{@module_name.underscore}.yml" %> file
18
+ flex.load_search_source
19
+
20
+ # you may need to add more method here, usually custom scopes
21
+ # or wrapper methods that use one of the autogenerated methods from the loaded templates
22
+
23
+ end
@@ -0,0 +1,22 @@
1
+ # Add here your search queries
2
+ # see the detailed Source documentation at http://ddnexus.github.io/flex/doc/2-flex/2-Templating/2-Sources.html
3
+
4
+
5
+ # ANCHORS litheral key: it will not be used as template
6
+ # you can store here fragments of queries to reuse in the templates below
7
+ ANCHORS:
8
+ -
9
+
10
+ # this is a a basic template loaded in your class <%= @module_name %>
11
+ # it defines the 'q' tag. You can check its usage from the rails console by just typing:
12
+ # >> puts <%= @module_name %>.flex.doc
13
+ # You can eventually restrict the doc to a single method by pasing its name:
14
+ # >> puts <%= @module_name %>.flex.doc :search
15
+ # see the detailed doc for this feature at http://ddnexus.github.io/flex/doc/2-flex/4-Self-Documentation.html
16
+
17
+ # the :cleanable_query is a special variable that get cleaned up if contains illegal characters for the Lucene syntax
18
+ search:
19
+ - query:
20
+ query_string:
21
+ query: <<cleanable_query= "*">>
22
+
@@ -0,0 +1,17 @@
1
+ # see the detailed Extenders documentation at http://ddnexus.github.io/flex/doc/2-flex/3-Result-Extenders.html
2
+
3
+ module <%= @extender_name %>
4
+
5
+ # set this method to restrict this extender to certain types of results
6
+ # see the other Flex extenders for reference
7
+ def self.should_extend?(response)
8
+ false
9
+ end
10
+
11
+ # adds your methods
12
+
13
+ def an_extending_method
14
+ # self is the structure returned by ES
15
+ end
16
+
17
+ end
@@ -0,0 +1,54 @@
1
+ # see the detailed Configuration documentation at http://ddnexus.github.io/flex/doc/1-Global-Doc/3-Configuration.html
2
+
3
+ Flex::Configuration.configure do |config|
4
+
5
+ # you must add your indexed model names here
6
+ config.flex_models = %w[ ]
7
+
8
+ # you must add your active model names here
9
+ config.flex_active_models = %w[ ]
10
+
11
+ # Add the your result extenders here
12
+ config.result_extenders |= [ <%= "#{@extender_name}" %> ]
13
+
14
+ # Add the default variables here:
15
+ # see also the details Variables documentation at http://ddnexus.github.io/flex/doc/2-flex/2-Templating/4-Variables.html
16
+ # config.variables.deep_merge! :index => 'my_index',
17
+ # :type => 'project',
18
+ # :anything => 'anything
19
+
20
+ # used by the live-reindex feature
21
+ # if you change it you must deploy the change first, then you can live-reindex in any next deploy
22
+ # read the documentation at http://ddnexus.github.io/flex/doc/2-flex/7-Live-Reindex.html#flexconfigurationapp_id
23
+ config.app_id = '<%= Time.now.strftime('%Y%m%d%H%M%S_') + Rails.application.class.name.split('::').first.underscore %>_' + Rails.env
24
+
25
+ # used by the live-reindex feature
26
+ # Uncomment and set the redis client only if you don't want to use the redis default client
27
+ # config.redis = Redis.new(:host => somehost, :port: 6379, :db = 1)
28
+
29
+
30
+ # Logger variables: see http://ddnexus.github.io/flex/doc/1-Global-Doc/3-Configuration.html
31
+ # config.logger.debug_variables = false
32
+ # config.logger.debug_request = false
33
+ # config.logger.debug_result = false
34
+ # config.logger.curl_format = false
35
+
36
+ # Custom config file path
37
+ # config.config_file = '/custom/path/to/flex.yml',
38
+
39
+ # Custom flex dir path
40
+ # config.flex_dir = '/custom/path/to/flex',
41
+
42
+ # The custom http_client you may want to implement
43
+ # config.http_client = Your::Client.new
44
+
45
+ # The custom url of your ElasticSearch server
46
+ # config.http_client.base_uri = 'http://localhost:9200'
47
+
48
+ # The options passed to the http_client. They are client specific.
49
+ # config.http_client.options = {:timeout => 5}
50
+
51
+ # Experimental: checks the response and returns a boolean (should raise?)
52
+ # config.http_client.raise_proc = proc{|response| response.status >= 400}
53
+
54
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flex-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Domizio Demichelis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '2.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: flex
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - '='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.0.1
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - '='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.0.1
46
+ - !ruby/object:Gem::Dependency
47
+ name: flex-models
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - '='
52
+ - !ruby/object:Gem::Version
53
+ version: 1.0.1
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.1
62
+ - !ruby/object:Gem::Dependency
63
+ name: prompter
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 0.1.5
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 0.1.5
78
+ description: Provides the engine, generators and helpers to integrate Flex with Rails
79
+ email: dd.nexus@gmail.com
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files:
83
+ - README.md
84
+ files:
85
+ - LICENSE
86
+ - README.md
87
+ - VERSION
88
+ - flex-rails.gemspec
89
+ - lib/flex-rails.rb
90
+ - lib/flex/rails/engine.rb
91
+ - lib/flex/rails/helper.rb
92
+ - lib/flex/rails/logger.rb
93
+ - lib/flex/result/rails_helper.rb
94
+ - lib/generators/flex/setup/setup_generator.rb
95
+ - lib/generators/flex/setup/templates/flex_config.yml
96
+ - lib/generators/flex/setup/templates/flex_dir/es.rb.erb
97
+ - lib/generators/flex/setup/templates/flex_dir/es.yml.erb
98
+ - lib/generators/flex/setup/templates/flex_dir/es_extender.rb.erb
99
+ - lib/generators/flex/setup/templates/flex_initializer.rb.erb
100
+ homepage: http://github.com/ddnexus/flex-rails
101
+ licenses: []
102
+ post_install_message:
103
+ rdoc_options:
104
+ - --charset=UTF-8
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: 1.3.6
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 1.8.25
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: Rails integration for Flex
125
+ test_files: []