lazy_elements 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 40b77d9a4de7925e1012db92cdeada3fd47050b0a115bbcc8a24ca6ea537c928
4
+ data.tar.gz: 711ba6b39c27e9316eee94db5473de01e2e47b2539d2b023cab0813d0a8e1b09
5
+ SHA512:
6
+ metadata.gz: 4145a3ba3ef26f37c6161130dde29690e6d7b8f029a4ac09ebffb945d6bbdc4f082321d6e51aee335f3f92f5c76eab3ef947a2d09d3fe7cc735f11de0d24df29
7
+ data.tar.gz: a935f8e2de815d39b0d72157cb329dd4d1c8ccc5b1ecccbb45a7bc006649a1eb6cb19ffda2420ef9c829cbc7f6e2eb429124b0e6cd67b304016c7f373e359e1c
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.4
5
+ before_install: gem install bundler -v 1.16.0
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in lazy_elements.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lazy_elements (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.7.0)
12
+ rspec-core (~> 3.7.0)
13
+ rspec-expectations (~> 3.7.0)
14
+ rspec-mocks (~> 3.7.0)
15
+ rspec-core (3.7.0)
16
+ rspec-support (~> 3.7.0)
17
+ rspec-expectations (3.7.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.7.0)
20
+ rspec-mocks (3.7.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.7.0)
23
+ rspec-support (3.7.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.16)
30
+ lazy_elements!
31
+ rake (~> 10.0)
32
+ rspec (~> 3.0)
33
+
34
+ BUNDLED WITH
35
+ 1.16.0
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # LazyElements
2
+
3
+ LazyElements is a gem that can lazy load html template through api.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'lazy_elements'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install lazy_elements
20
+
21
+ Run
22
+ $ rails g lazy_elements:install
23
+
24
+
25
+ Add the following to application.js
26
+
27
+ ```
28
+ //=require lazy_elements
29
+ ```
30
+
31
+
32
+ ## Basic Usage
33
+
34
+ In our view file write
35
+
36
+ ```
37
+ = lazy_element(:your_partial_template_name)
38
+ ```
39
+
40
+ And add template to app/view/api/lazy_elements/elements/your_partial_template_name
41
+
42
+ ### Use Attributes
43
+
44
+ If you want to pass a value for template, you can use hash in the name of attr
45
+
46
+
47
+ app/views/posts/show.html.haml
48
+
49
+ ```
50
+ = lazy_element(:lazy_template, {attr: {id: 1, name: 'hoge'}})
51
+ ```
52
+
53
+ app/view/api/lazy_elements/elements/_lazy_template.html.haml
54
+ ```
55
+ .test_template
56
+ = attr[:id]
57
+ = attr[:name]
58
+ ```
59
+
60
+
61
+
62
+
63
+ ## Configuration
64
+
65
+ You are available some options
66
+
67
+ - template_path
68
+ - class_name
69
+
70
+ config/initializers/lazy_elements.rb
71
+
72
+ ```ruby
73
+ LazyElements.configure do |config|
74
+ config.template_path = 'your template path'
75
+ config.class_name = 'your class name'
76
+ end
77
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,19 @@
1
+ (function($) {
2
+ var ready = function(e) {
3
+ $('.<%= LazyElements.config.class_name %>').each(function(i, e) {
4
+ var id = $(e).data('id');
5
+ var attr = $(e).data('attr');
6
+
7
+ var params_h = {id: id, attr: attr}
8
+ var params = jQuery.param(params_h)
9
+ var req_url = '<%= LazyElements.config.controller_path %>' + '?' + params
10
+
11
+ $.get(req_url, function(json) {
12
+ var html = json['template']
13
+ $('.<%= LazyElements.config.class_name %>[data-id='+id+']').html(html);
14
+ });
15
+ });
16
+ }
17
+
18
+ $(document).ready(ready);
19
+ })(jQuery);
@@ -0,0 +1,10 @@
1
+ class Api::LazyElements::ElementsController < ApplicationController
2
+ def show
3
+ name = params[:id]
4
+ attr = params[:attr]
5
+
6
+ file_path = "#{LazyElements.config.template_path}#{name}"
7
+ template_str = render_to_string(partial: file_path, locals: {attr: attr})
8
+ render json: {template: template_str}
9
+ end
10
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "lazy_elements"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "lazy_elements/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "lazy_elements"
8
+ spec.version = LazyElements::VERSION
9
+ spec.authors = ["山野井侑"]
10
+ spec.email = ["yuu.yamanoi1222@gmail.com"]
11
+
12
+ spec.summary = %q{lazy load html element}
13
+ spec.description = spec.summary
14
+ spec.homepage = "https://github.com/yyamanoi1222/lazy_elements"
15
+
16
+ if spec.respond_to?(:metadata)
17
+ else
18
+ raise "RubyGems 2.0 or newer is required to protect against " \
19
+ "public gem pushes."
20
+ end
21
+
22
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
23
+ f.match(%r{^(test|spec|features)/})
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.16"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "rspec", "~> 3.0"
32
+ end
@@ -0,0 +1,17 @@
1
+ module LazyElements
2
+ module Generators
3
+ class InstallGenerator < ::Rails::Generators::Base
4
+ desc "This generator write a route for lazy_elements api"
5
+
6
+ def write_lazy_elements_route
7
+ lazy_elements_route = "namespace :api do\n"
8
+ lazy_elements_route << " namespace :lazy_elements do\n"
9
+ lazy_elements_route << " resource :elements"
10
+ lazy_elements_route << ", only: :show\n"
11
+ lazy_elements_route << " end\n"
12
+ lazy_elements_route << " end"
13
+ route lazy_elements_route
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ require "lazy_elements/version"
2
+ require "lazy_elements/config"
3
+ require "lazy_elements/engine"
4
+ require "lazy_elements/view_helpers"
5
+
6
+ module LazyElements
7
+ end
8
+
9
+ ActionView::Base.send :include, LazyElements::ViewHelpers
@@ -0,0 +1,37 @@
1
+ require 'active_support/configurable'
2
+
3
+ module LazyElements
4
+ def self.default_controller_path
5
+ '/api/lazy_elements/elements'
6
+ end
7
+
8
+ def self.default_template_path
9
+ 'api/lazy_elements/elements/'
10
+ end
11
+
12
+ def self.default_class_name
13
+ 'lazy_element'
14
+ end
15
+
16
+ def self.configure(&block)
17
+ yield @config ||= LazyElements::Configuration.new
18
+ end
19
+
20
+ def self.config
21
+ @config
22
+ end
23
+
24
+ class Configuration
25
+ include ActiveSupport::Configurable
26
+ config_accessor :controller_path
27
+ config_accessor :template_path
28
+ config_accessor :class_name
29
+ end
30
+
31
+ configure do |config|
32
+ config.controller_path = LazyElements.default_controller_path
33
+ config.template_path = LazyElements.default_template_path
34
+ config.class_name = LazyElements.default_class_name
35
+ end
36
+ end
37
+
@@ -0,0 +1,3 @@
1
+ module LazyElements
2
+ class Engine < Rails::Engine; end
3
+ end
@@ -0,0 +1,3 @@
1
+ module LazyElements
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,31 @@
1
+ module LazyElements
2
+ module ViewHelpers
3
+ def lazy_element(name, options = {})
4
+ check_template_file!(name)
5
+ render_lazy_blank_element(name, options[:attr])
6
+ end
7
+
8
+ def render_lazy_blank_element(name, attr)
9
+ default_option = {'data-id' => name, 'class' => LazyElements.config.class_name}
10
+
11
+ option = default_option
12
+
13
+ if attr.is_a?(Hash)
14
+ option['data-attr'] = attr.to_json
15
+ end
16
+
17
+
18
+ content_tag(:div, '', option)
19
+ end
20
+
21
+ private
22
+
23
+ def check_template_file!(name)
24
+ path = LazyElements.config.template_path
25
+
26
+ unless lookup_context.exists?(name.to_s, path, true)
27
+ raise ActionView::ActionViewError.new("Template missing #{name.to_s} in #{path}")
28
+ end
29
+ end
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lazy_elements
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - "山野井侑"
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-02-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: lazy load html element
56
+ email:
57
+ - yuu.yamanoi1222@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - README.md
68
+ - Rakefile
69
+ - app/assets/javascripts/lazy_elements.js.erb
70
+ - app/controller/api/lazy_elements/elements_controller.rb
71
+ - bin/console
72
+ - bin/setup
73
+ - lazy_elements.gemspec
74
+ - lib/generators/lazy_elements/install/install_generator.rb
75
+ - lib/lazy_elements.rb
76
+ - lib/lazy_elements/config.rb
77
+ - lib/lazy_elements/engine.rb
78
+ - lib/lazy_elements/version.rb
79
+ - lib/lazy_elements/view_helpers.rb
80
+ homepage: https://github.com/yyamanoi1222/lazy_elements
81
+ licenses: []
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.7.6
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: lazy load html element
103
+ test_files: []