Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0c73d02

Browse files
- Added activerecord-import gem to bulk import
- Added activerecord transaction and used bulk import of data content
1 parente93d8a0 commit0c73d02

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

‎Gemfile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ gem 'rails', '~> 5.2.0'
1212
gem'mysql2','~> 0.5.1'
1313
# Use Puma as the app server
1414
gem'puma','~> 3.7'
15+
gem'activerecord-import','~> 0.23.0'
1516
gem'jsonapi-resources','~> 0.9.0'
1617
gem'httparty','~> 0.16.2'
1718
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder

‎Gemfile.lock‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ GEM
3333
activemodel (=5.2.0)
3434
activesupport (=5.2.0)
3535
arel (>=9.0)
36+
activerecord-import (0.23.0)
37+
activerecord (>=3.2)
3638
activestorage (5.2.0)
3739
actionpack (=5.2.0)
3840
activerecord (=5.2.0)
@@ -137,6 +139,7 @@ PLATFORMS
137139
ruby
138140

139141
DEPENDENCIES
142+
activerecord-import (~>0.23.0)
140143
byebug
141144
httparty (~>0.16.2)
142145
jsonapi-resources (~>0.9.0)

‎app/controllers/contents_controller.rb‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@ def parse
77
response=HTTParty.get(params[:ref_url])
88
doc=Nokogiri::HTML(response.body)
99

10+
contents=[]
1011
%w[h1h2h3a].eachdo |tag|
1112
doc.xpath("//#{tag}").eachdo |element|
12-
extract_element(element,tag)
13+
content=Content.new
14+
content.url=params[:ref_url]
15+
content.tag=tag
16+
content.content=element.text
17+
contents <<content
1318
end
1419
end
1520

21+
Content.transactiondo
22+
Content.import!contents,validate:false
23+
end
24+
1625
renderjson:{success:true,code:response.code,message:response.message}
1726
rescueException=>e
1827
renderjson:e
1928
end
2029
end
21-
22-
private
23-
defextract_element(element,tag='')
24-
content=Content.new
25-
content.url=params[:ref_url]
26-
content.tag=tag
27-
content.content=element.text
28-
content.save!
29-
end
3030
end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp